在Python中调用具有参考参数的.Net函数 [英] Calling a .Net function in Python which has a reference parameter

查看:74
本文介绍了在Python中调用具有参考参数的.Net函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在VS2012中使用 IronPython ,并尝试调用一个采用参考参数

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)

我遗漏了一些东西,在C#中,我们将ref与变量名一起使用,在Python中呢?

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

推荐答案

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

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

在第一种情况下,呼叫是通过自动编组处理的.返回值和更改的ref都包装在一个元组中,并且(例如,以 15.1 作为要传递的双精度值)可以像下面这样使用:

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)

更明确的方法是提供准备好的clr引用:

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

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

这篇关于在Python中调用具有参考参数的.Net函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆