在我看到的C# scripts using DynamicMethod Pluse一文中描述了这一点--第一次调用比使用CSharpCodeProvider快得多。
这种方法的缺点是什么?
发布于 2011-06-22 18:42:53
我刚读完C# Scripts using DynamicMethod的源代码
我认为最不能容忍的缺点是:太复杂了。在.Net 4.0中,我们可以像使用DynamicMethod一样,使用DLR和ironpython5%的代码行来编写脚本。DLR是较新的,这是趋势。
DLR和IronPython一些代码示例:
var scriptEngine = Python.CreateEngine();
var scriptSource = scriptEngine.CreateScriptSourceFromString(@"# coding=utf-8
def execute(command):
exec(command)
");
scriptScope = scriptEngine.CreateScope();
scriptSource.Execute(scriptScope);
dynamic execute = scriptScope.GetVariable("execute");
execute("print 'hello world'")只有伪代码,你必须修改上面的代码才能编译和运行。我写上面的代码是为了向你展示如果你使用DLR和Ironpython而不是DynamicMethod是多么容易。
发布于 2011-06-25 00:43:19
DynamicMethod需要直接编写IL。这篇文章的作者显然已经编写了自己的编译器来将C#脚本转换为可以加载到DynamicMethod中的IL,但这很可能非常脆弱。CSharpCodeProvider使用csc,与在Visual Studio (almost)中运行的编译器相同,因此它可能更可靠。
https://stackoverflow.com/questions/6169393
复制相似问题