现在我可以使用下面的代码在 IronPython
中访问我的 c#
类型,如下所示
import clr
clr.AddReference('myDLL.dll')
import myType
obj = myType()
但是我不希望脚本开发人员在 Python
源代码中有 clr.AddReference('myDLL.dll')
行,并注入(inject) myDLL。 dll
(和/或 c#
类)直接从 c#
到 ScriptEngine,因此之前的代码类似于:
import myType
obj = myType()
我怎样才能做到这一点?
最佳答案
您可以使用以下解决方案解决此问题:
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()
关于c# - 将 c# 类型注入(inject) Ironpython,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44603598/