相关疑难解决方法(0)

使用DLR运行用CompileAssemblyFromSource生成的代码?

继续这个优秀的答案,我想知道使用dynamic关键字的DLR是否允许更简洁的方式为生成的程序集编写代码.

例如,上述答案的代码可以:

using (Microsoft.CSharp.CSharpCodeProvider foo = 
           new Microsoft.CSharp.CSharpCodeProvider())
{
    var res = foo.CompileAssemblyFromSource(
        new System.CodeDom.Compiler.CompilerParameters() {  
            GenerateInMemory = true 
        }, 
        "public class FooClass { public string Execute() { return \"output!\";}}"
    );

    var type = res.CompiledAssembly.GetType("FooClass");
    var obj = Activator.CreateInstance(type);
    var output = type.GetMethod("Execute").Invoke(obj, new object[] { });
}
Run Code Online (Sandbox Code Playgroud)

变成这样的东西:

using (Microsoft.CSharp.CSharpCodeProvider foo = 
           new Microsoft.CSharp.CSharpCodeProvider())
{
    var res = foo.CompileAssemblyFromSource(
        new System.CodeDom.Compiler.CompilerParameters() {  
            GenerateInMemory = true 
        }, 
        "public class FooClass { public string Execute() { return …
Run Code Online (Sandbox Code Playgroud)

.net c# codedom dynamic-language-runtime

5
推荐指数
1
解决办法
1802
查看次数

标签 统计

.net ×1

c# ×1

codedom ×1

dynamic-language-runtime ×1