gpt4 book ai didi

.net - 在 Python 中调用具有引用参数的 .Net 函数

转载 作者:行者123 更新时间:2023-12-03 23:46:07 29 4
gpt4 key购买 nike

我正在使用 IronPython在 VS2012 中并尝试调用一个接受 Ref 的 .Net 函数范围,

lib.dll

public int GetValue(ref double value)
{
...
}

python :
import clr
clr.AddReference('Lib.dll')
from LibDll import *

value =0.0
x = GetValue(value)

我是否遗漏了什么,在 C# 中我们使用 ref连同变量名,在 Python 中呢?

最佳答案

有两种方法可以从 IronPython 调用带有 out 或 ref 参数的方法。

在第一种情况下,调用由自动编码处理。返回值和更改的 refs 包含在一个元组中,并且(以 15.1 作为要传递的 double 示例)可以像这样使用:

(returned, referenced) = GetValue(15.1)

更明确的方法是提供一个准备好的 clr 引用:
refParam = clr.Reference[System.Double](15.1)
result = GetValue(refParam)

关于.net - 在 Python 中调用具有引用参数的 .Net 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26998687/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com