Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在(Iron)Python脚本中引用宿主程序集失败_C#_Ironpython - Fatal编程技术网

C# 在(Iron)Python脚本中引用宿主程序集失败

C# 在(Iron)Python脚本中引用宿主程序集失败,c#,ironpython,C#,Ironpython,我目前正在测试IronPython(我知道一点C和一点CPython)。现在我想在Python脚本中使用我的自定义类 我有以下项目结构: Solution IPTest ---Project IPTest ------Namespace IPTest ---------Program class (main) ---------Elem class ------Scripts ---------Worker.py 其中Elem是一个简单的: public class Elem { pu

我目前正在测试IronPython(我知道一点C和一点CPython)。现在我想在Python脚本中使用我的自定义类

我有以下项目结构:

Solution IPTest
---Project IPTest
------Namespace IPTest
---------Program class (main)
---------Elem class
------Scripts
---------Worker.py
其中Elem是一个简单的:

public class Elem {
    public string Name;
}
我可以在.NET程序中使用Python脚本,而不会出现以下问题:

ScriptEngine engine = Python.CreateEngine();
ScriptSource source = engine.CreateScriptSourceFromFile("./Scripts/Worker.py");
ScriptScope scope = engine.CreateScope();
source.Execute(scope);
// or var worker = Python.CreateRuntime().UseFile("./Scripts/Worker.py");
dynamic Worker = scope.GetVariable("Worker");
dynamic worker = Worker();
var res1 = worker.add(4, 5);
但是,我不知道如何在Python脚本中引用宿主程序集。经过一些研究,我尝试了以下方法:

import sys
import System
sys.path.append(System.IO.Directory.GetCurrentDirectory()) #make sure assembly dir is  in sys.path
import clr
clr.AddReference(“IPTest.exe”)
# or clr.AddReferenceToFile(r"IPTest.exe")
# or clr.AddReference(r"<fullpath>\IPTest\bin\Debug\IPTest.exe")
# or clr.AddReference(“../IPTest.exe”) #when not adding workingdir to sys.path
from IPTest import Elem
# or from IPTest.IPTest import Elem
# or import Elem
很好用。但是,我认为它也应该通过在脚本文件中引用来工作(这样会更好)


可能我遗漏了一些明显的东西,但我就是看不到,所以任何提示都非常感谢。

请尝试
clr.AddReferenceToFile(r'IPTest.exe')
,如错误中所述:

  • 将workingdir添加到sys.path或使用相对路径或使用AddReferenceToFile时:没有名为IPTest的模块

  • 我快疯了。。。我尝试了
    clr.AddReferenceToFile(r“IPTest.exe”)
    (带“than”)-这不起作用。但是使用“一切都运行顺利。无论如何,谢谢”。
    engine.Runtime.LoadAssembly(Assembly.GetExecutingAssembly()); //in c#
    from IPTest import Elem # in python script