您的位置:首页 > 编程语言 > C#

c#动态编译

2016-03-23 23:57 453 查看
本文摘抄自 http://www.cnblogs.com/jailu/archive/2007/07/22/827058.html
为加深记忆,自己整理代码。

还有个winform的代码链接,没有仔细研究。http://blog.csdn.net/bdmh/article/details/22398077

using System;
using System.Reflection;
using System.Globalization;
using Microsoft.CSharp;
using System.CodeDom;
using System.CodeDom.Compiler;
using System.Text;

namespace DongTaiClass
{
class Program
{
static void Main(string[] args)
{

// 1.CSharpCodePrivoder
CSharpCodeProvider objCSharpCodePrivoder = new CSharpCodeProvider();

// 2.ICodeComplier
//ICodeCompiler objICodeCompiler = objCSharpCodePrivoder.CreateCompiler();已过时,不用获取ICodeCompiler接口,直接使用objCSharpCodePrivoder

#region 编译参数
// 3.CompilerParameters
//定义编译参数
CompilerParameters objCompilerParameters = new CompilerParameters();
//引用的程序集(在此添加)
objCompilerParameters.ReferencedAssemblies.Add("System.dll");
//如果要添加相对位置dll可这样添加
//string path = "";
//try
//{
//    path = AppDomain.CurrentDomain.BaseDirectory + "bin\\";
//}
//catch { }
//objCompilerParameters.ReferencedAssemblies.Add(path + "Penseesoft.Bonus.BLL.dll");

//是否生成exe程序 true:生成exe文件,则代码中要有静态的main方法
//                false:生成dll文件
objCompilerParameters.GenerateExecutable = false;
//生成结果保存方式 true:在内存中生成
//                 false:需要指定具体的地址 objCompilerParameters.OutputAssembly= "e:\\a.dll";
objCompilerParameters.GenerateInMemory = true;

#endregion 编译参数

// 4.CompilerResults 编译文本代码
CompilerResults cr = objCSharpCodePrivoder.CompileAssemblyFromSource(objCompilerParameters, new string[] { GenerateCode() });
if (cr.Errors.HasErrors)
{
Console.WriteLine("编译错误:");
foreach (CompilerError err in cr.Errors)
{
Console.WriteLine(err.ErrorText);
}
}
else
{
// 通过反射,调用HelloWorld的实例
Assembly objAssembly = cr.CompiledAssembly;
object objHelloWorld = objAssembly.CreateInstance("DongTaiClass.HelloWorld"); //相当于 HelloWorld objHelloWorld = new HelloWorld()
//调用OutPut方法,参数为null,返回 return \"Hello world!\";
MethodInfo objMI = objHelloWorld.GetType().GetMethod("OutPut");
Console.WriteLine(objMI.Invoke(objHelloWorld, null));
//调用jisuan方法,参数为1,2,返回 1+2=3
MethodInfo objJS = objHelloWorld.GetType().GetMethod("jisuan");
Console.WriteLine(objJS.Invoke(objHelloWorld, new object[] { 1, 2 }));

//这里不推荐使用构造函数,可以调用某个方法来初始化数据
//这里可以直接使用objHelloWorld实例
}

Console.ReadLine();
}

static string GenerateCode()
{
StringBuilder sb = new StringBuilder();
sb.AppendLine("using System;");
sb.AppendLine("namespace DongTaiClass");
sb.AppendLine("{");
sb.AppendLine("    public class HelloWorld");
sb.AppendLine("    {");
sb.AppendLine("        public string OutPut()");
sb.AppendLine("        {");
sb.AppendLine("             return \"Hello world!\";");
sb.AppendLine("        }");
sb.AppendLine("        public int jisuan(int a,int b)");
sb.AppendLine("        {");
sb.AppendLine("            return a + b ;");
sb.AppendLine("        }");
sb.AppendLine("    }");
sb.AppendLine("}");

string code = sb.ToString();
Console.WriteLine(code);
Console.WriteLine();

return code;
}
}
}


  上图
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: