【发布时间】:2010-06-09 19:30:05
【问题描述】:
我正在尝试使用 IronPython 作为 C# GUI 和一些 C# 库之间的中介,以便可以在编译后编写脚本。
我有一个 GUI 和 python 都使用的类库 DLL,它是这样的:
namespace MyLib
{
public class MyClass
{
public string Name { get; set; }
public MyClass(string name)
{
this.Name = name;
}
}
}
IronPython代码如下:
import clr
clr.AddReferenceToFile(r"MyLib.dll")
from MyLib import MyClass
ReturnObject = MyClass("Test")
然后,在 C# 中我会这样称呼它:
ScriptEngine engine = Python.CreateEngine();
ScriptScope scope = null;
scope = engine.CreateScope();
ScriptSource source = engine.CreateScriptSourceFromFile("Script.py");
source.Execute(scope);
MyClass mc = scope.GetVariable<MyClass>("ReturnObject ")
当我调用这最后一段代码时,source.Execute(scope) 运行成功返回,但是当我尝试调用 GetVariable 时,它会抛出以下异常
Microsoft.Scripting.ArgumentTypeException: expected MyClass , got MyClass
因此,您可以看到类名完全相同,但由于某种原因它认为它们不同。
DLL 与 .py 文件位于不同的目录中(我只是懒得写出所有路径设置的东西),可能是 IronPython 的解释器将这些对象视为差异存在问题因为它以某种方式将它们视为处于不同的上下文或范围?
【问题讨论】:
标签: c# interop ironpython c#-4.0