【问题标题】:How to call a C# library function with ref and out parameter from python?如何从python调用带有ref和out参数的C#库函数?
【发布时间】:2018-08-15 05:22:59
【问题描述】:

我使用 pythonnet 而不是 ironpython。

有这样一个函数:

test(ref string p1,out string p2)

如何在 python 3.6 中调用test

import clr
import sys
import System
sys.path.append(r'd:\dll')  
clr.FindAssembly('communication.dll')  
from  communication import *  
dll=IEC62056()
dll.test(----------)

【问题讨论】:

  • 你可以查看类似的讨论here
  • 嗯不是最好的解决方案,但为什么不制作一个控制台程序来包装你的 dll,然后从 python 调用控制台程序?
  • @Rahul Agarwal 这个页面是 iropython 而不是 pythonnet pythonnet.github.io
  • @Agent_Orange 对不起,我听不懂,你能给我举个例子吗?

标签: python .net ref python.net pythonnet


【解决方案1】:

没有communication.dll我无法测试代码,所以请检查以下代码是否适合您:

import clr
import sys
sys.path.append("D:\\dll") # path to dll
clr.AddReference("communication") # add reference to communication.dll
from  communication import test

ref_string_p1 = "----------"
out_string_p2 = test(ref_string_p1)
print(out_string_p2)

【讨论】:

    【解决方案2】:

    您可以将 ref 作为常规参数输入,并且可以任意输出。 pythonnet 会将它们按位置作为元组返回。

    在 c# 中假设 void test(ref string p1,out string p2), 你在 python 中得到p1, p2 = test(p1, None)

    【讨论】:

      猜你喜欢
      • 2010-09-16
      • 1970-01-01
      • 2013-04-18
      • 1970-01-01
      • 1970-01-01
      • 2011-07-26
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多