使用 CSharpCodeProvider 进行 C# 运行时编译
C# Runtime Compilation with CSharpCodeProvider
我使用本教程取得了成功:http://www.codeproject.com/Tips/715891/Compiling-Csharp-Code-at-Runtime 建立了运行时编译和执行 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
class 有另一个方法 returnsTwo()
,其中 returns 整数 2。有没有办法通过传递 "CodeCompiler.returnsTwo();"
或类似于 InterpretString()
?
的字符串
前提是函数是静态函数这个应该问题不大,只要在编译时添加适当的引用即可。我在几个项目上都做过这件事。
如果 CodeCompiler 在您当前的可执行文件中,您必须以这种方式包含引用:
string exePath = Assembly.GetExecutingAssembly().Location;
string exeDir = Path.GetDirectoryName(exePath);
AssemblyName[] assemRefs = Assembly.GetExecutingAssembly().GetReferencedAssemblies();
List<string> references = new List<string>();
foreach (AssemblyName assemblyName in assemRefs)
references.Add(assemblyName.Name + ".dll");
for (int i = 0; i < references.Count; i++)
{
string localName = Path.Combine(exeDir, references[i]);
if (File.Exists(localName))
references[i] = localName;
}
references.Add(exePath);
CompilerParameters compiler_parameters = new CompilerParameters(references.ToArray())
我使用本教程取得了成功:http://www.codeproject.com/Tips/715891/Compiling-Csharp-Code-at-Runtime 建立了运行时编译和执行 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
class 有另一个方法 returnsTwo()
,其中 returns 整数 2。有没有办法通过传递 "CodeCompiler.returnsTwo();"
或类似于 InterpretString()
?
前提是函数是静态函数这个应该问题不大,只要在编译时添加适当的引用即可。我在几个项目上都做过这件事。
如果 CodeCompiler 在您当前的可执行文件中,您必须以这种方式包含引用:
string exePath = Assembly.GetExecutingAssembly().Location;
string exeDir = Path.GetDirectoryName(exePath);
AssemblyName[] assemRefs = Assembly.GetExecutingAssembly().GetReferencedAssemblies();
List<string> references = new List<string>();
foreach (AssemblyName assemblyName in assemRefs)
references.Add(assemblyName.Name + ".dll");
for (int i = 0; i < references.Count; i++)
{
string localName = Path.Combine(exeDir, references[i]);
if (File.Exists(localName))
references[i] = localName;
}
references.Add(exePath);
CompilerParameters compiler_parameters = new CompilerParameters(references.ToArray())