gpt4 book ai didi

c# - 使用 DynamicMethod 编写脚本

转载 作者:太空狗 更新时间:2023-10-29 17:57:38 27 4
gpt4 key购买 nike

这在文章 C# scripts using DynamicMethod 中有描述我看到的优点 - 第一次调用将比使用 CSharpCodeProvider 快得多。

这种方法有什么缺点?

最佳答案

刚看完C# Scripts using DynamicMethod的源码

我认为最不能容忍的缺点是:太复杂了。在 .Net 4.0 中,我们可以使用 DLR 和 ironpython 编写脚本,只需 5% 的代码行即可使用 DynamicMethod。 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 会有多么容易。

关于c# - 使用 DynamicMethod 编写脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6169393/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com