DLR的应用:托管API与面向切面编程
1. DLR托管API
DLR(动态语言运行时)托管API提供了丰富的价值和功能。下面是一个使用DLR托管API运行Python代码的示例:
RemotingFactory remotingFactory = (RemotingFactory)Activator.GetObject(
typeof(RemotingFactory), "tcp://localhost:8088/RemotingFactory");
ScriptRuntime runtime = remotingFactory.GetScriptRuntime();
ScriptEngine pyEngine = runtime.GetEngine("python");
string pyFunc = @"def isodd(n): return 1 == n % 2;";
ScriptSource source = pyEngine.CreateScriptSourceFromString(pyFunc,
SourceCodeKind.Statements);
ScriptScope scope = pyEngine.CreateScope();
source.Execute(scope);
ObjectHandle IsOddHandle = scope.GetVariableHandle("isodd");
ObjectHandle result = pyEngine.Operations.Invoke(IsOddHandle, new object[] { 3 });
bool answer = pyEngine.
超级会员免费看
订阅专栏 解锁全文
5

被折叠的 条评论
为什么被折叠?



