Calling a .Net function in Python which has a reference parameter

随声附和 提交于 2020-01-13 10:17:10

问题


I'm using IronPython in VS2012 and trying to call a .Net function which takes in a Ref parameter,

Lib.dll

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

Python:

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

value =0.0
x = GetValue(value)

am I missing something, in C# we use ref along with the variable name, what about here in Python?


回答1:


There are two ways you can invoke methods with out or ref parameters from IronPython.

In the first case the call is handled by automatic marshalling. The return value and changed refs are wrapped in a tuple and (while having 15.1 as an example double to be passed) can be used like:

(returned, referenced) = GetValue(15.1)

The more explicit way is providing a prepared clr-reference:

refParam = clr.Reference[System.Double](15.1)
result = GetValue(refParam)


来源:https://stackoverflow.com/questions/26998687/calling-a-net-function-in-python-which-has-a-reference-parameter

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!