南昌大学|中正论坛

 找回密码
 注册[30秒完成]
搜索
查看: 5094|回复: 0

两个C#动态编译代码的例子

[复制链接]
发表于 2008-9-9 01:28:27 | 显示全部楼层 |阅读模式
动态生成代码程序收集NO1:

using System;
using System.Collections.Generic;
using System.Reflection;
using System.Text;
using System.CodeDom;
using System.CodeDom.Compiler;



namespace csrepl {
class Program {



static string funcprefix = "using System; "
+ "public delegate void Proc(); "
+ "public class Wrapper { "
+ " public static object Set(string name, object value) { "
+ " AppDomain.CurrentDomain.SetData(name, value); "
+ " return value; "
+ " } "
+ " public static object Get(string name) { "
+ " return AppDomain.CurrentDomain.GetData(name); "
+ " } "
+ " public static object Invoke(Proc proc) { "
+ " proc(); "
+ " return null; "
+ " } "
+ " public static object Eval() { return ";
static string funcsuffix = "; } }";




static string StringEval(string expr) {
string program = funcprefix + expr + funcsuffix;



ICodeCompiler compiler = CodeDomProvider.CreateProvider("C#").CreateCompiler();
CompilerParameters cp = new CompilerParameters();
cp.GenerateExecutable = false;
cp.GenerateInMemory = true;



CompilerResults results = compiler.CompileAssemblyFromSource(cp, program);
if (results.Errors.HasErrors) {
if (results.Errors[0].ErrorNumber == "CS0029")
return StringEval("Invoke(delegate { " + expr + "; })");
return results.Errors[0].ErrorText;
}
else {
Assembly assm = results.CompiledAssembly;
Type target = assm.GetType("Wrapper");
MethodInfo method = target.GetMethod("Eval");
object result = method.Invoke(null, null);
return result == null ? null : result.ToString();
}
}



static void Main(string[] args) {



while (true ) {
Console.Write("> ");
Console.Out.Flush();
string expr = Console.ReadLine();
if (expr == null)
break;
try {
string result = StringEval(expr);
Console.WriteLine(result);
}
catch (TargetInvocationException ex) {
Console.WriteLine(ex.InnerException.GetType().Name + ": " + ex.InnerException.Message);
}
catch (Exception ex) {
Console.WriteLine(ex.GetType().Name + ": " + ex.Message);
}
}

}
}
}

代码2:

private void Compiler(string code)
{
CompilerParameters vCompilerParameters = new CompilerParameters();
vCompilerParameters.GenerateExecutable = false;
vCompilerParameters.GenerateInMemory = true;
vCompilerParameters.ReferencedAssemblies.Add("System.Windows.Forms.dll");
string vSource =
"using System.Windows.Forms;\n" +
"public class Temp\n" +
"{\n" +
" public void Test()\n" +
" {\n" +
" " + code + "\n" +
" }\n" +
"}\n";
CompilerResults vCompilerResults =
CodeDomProvider.CreateProvider("CSharp").CompileAssemblyFromSource(vCompilerParameters, vSource);

Assembly vAssembly = vCompilerResults.CompiledAssembly;
object vTemp = vAssembly.CreateInstance("Temp");
MethodInfo vTest = vTemp.GetType().GetMethod("Test");
vTest.Invoke(vTemp, null);
}



string code = "int sum = 0;\nfor(int i = 0; i < 100; i++)\n{\nsum += i;\n}\nMessageBox.Show(sum.ToString());";
Compiler(code);
您需要登录后才可以回帖 登录 | 注册[30秒完成]

本版积分规则

手机访问本页请
扫描左边二维码
         本网站声明
本网站所有内容为网友上传,若存在版权问题或是相关责任请联系站长!
站长联系QQ:7123767   myubbs.com
         站长微信:7123767
请扫描右边二维码
www.myubbs.com

小黑屋|手机版|Archiver|南昌大学论坛 ( 琼ICP备10001196号-2 )

GMT+8, 2024-3-28 18:03 , Processed in 0.117804 second(s), 16 queries .

Powered by 高考信息网 X3.3

© 2001-2013 大学排名

快速回复 返回顶部 返回列表