我正在尝试使用Iron
Python作为C#GUI和一些C#库之间的中介,以便它可以在编译后编写脚本.
我有一个类库DLL,由GUI和python使用,是这样的:
- 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的解释器存在问题,因为它看起来不同,因为它是某种方式看到他们处于不同的背景或范围?