2
我已经成功使用本教程:http://www.codeproject.com/Tips/715891/Compiling-Csharp-Code-at-Runtime为运行时编译和C#代码的执行设置框架。下面是我目前拥有的代码:使用CSharpCodeProvider进行C#运行时编译
public static class CodeCompiler {
public static object InterpretString(string executable) {
string compilation_string =
@"
static class RuntimeCompilationCode {
public static void Main() {}
public static object Custom() {
/* CODE HERE */
}
}";
compilation_string = compilation_string.Replace("/* CODE HERE */", executable);
CSharpCodeProvider provider = new CSharpCodeProvider();
CompilerParameters compiler_parameters = new CompilerParameters();
// True - memory generation, false - external file generation
compiler_parameters.GenerateInMemory = true;
// True - exe file generation, false - dll file generation
compiler_parameters.GenerateExecutable = true;
// Compile
CompilerResults results = provider.CompileAssemblyFromSource(compiler_parameters, compilation_string);
// Check errors
if (results.Errors.HasErrors) {
StringBuilder builder = new StringBuilder();
foreach (CompilerError error in results.Errors) {
builder.AppendLine(String.Format("Error ({0}): {1}", error.ErrorNumber, error.ErrorText));
}
throw new InvalidOperationException(builder.ToString());
}
// Execute
Assembly assembly = results.CompiledAssembly;
Type program = assembly.GetType("RuntimeCompilationCode");
MethodInfo execute = program.GetMethod("Custom");
return execute.Invoke(null, null);
}
}
我可以在一个字符串(前"return 2;"
)到InterpretString()
的形式通过一项声明,它会被编译并作为Custom()
功能的一部分执行。不过,我想知道是否可以使用相同的方法来执行我的原始文件中的方法。例如,假设CodeCompiler
类有另一种方法returnsTwo()
,它返回整数2.有没有办法调用"CodeCompiler.returnsTwo();"
或类似的字符串InterpretString()
来调用这样的方法?