现在,我可以使用以下代码访问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()
我怎样才能做到这一点?
答案 0 :(得分:5)
您可以使用以下解决方案解决此问题:
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()