作者:Hancl | 来源:互联网 | 2023-02-01 02:40
现在我可以使用以下代码来访问我的c#
类型,IronPython
如下所示
import clr
clr.AddReference('myDLL.dll')
import myType
obj = myType()
但是我不希望脚本开发人员clr.AddReference('myDLL.dll')
在Python
源代码中使用行,并将myDLL.dll
(和/或c#
类)直接c#
注入ScriptEngine,因此以前的代码类似于:
import myType
obj = myType()
我该怎么做到这一点?
1> 小智..:
您可以使用以下解决方案解决此问题:
ScriptRuntime runtime = Python.CreateRuntime();
runtime.LoadAssembly(Assembly.GetAssembly(typeof(MyNameSpace.MyClass)));
ScriptEngine eng = runtime.GetEngine("py");
ScriptScope scope = eng.CreateScope();
ScriptSource src = eng.CreateScriptSourceFromString(MySource, SourceCodeKind.Statements);
var result = src.Execute(scope);
现在,在python脚本中,您可以编写:
from MyNameSpace import *
n=MyClass()
print n.DoSomeThing()
谢谢,这正是我想要的