Python .net框架参考参数Double []& [英] Python .net framework reference argument Double[]&

查看:60
本文介绍了Python .net框架参考参数Double []&的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用Python for .Net框架,我试图从C#.dll文件中调用方法.此方法具有以下参数:

Using the Python for .Net framework I'm trying to call a method from a C# .dll file. This method has the following arguments:

public static void ExternalFunction(
    String Arg1,
    ref Double[]& Arg2,
);

我了解.Net框架将Python浮点数转换为双精度数.现在,我想知道如何制作一个数组(双精度)并将其作为对外部方法的引用.

I understood the .Net framework converts Python floats to doubles. Now I would like to know how to make an array (double) and pass this as a reference to the external method.

我有以下代码:

import clr

clr.AddReference("MyDll")
from MyLib import MyClass

myName = "Benjamin"

r = MyClass.ExternalFunction(myName, 0.0);
print "Result: %s"%r

推荐答案

您可以通过提供浮点列表(或元组)作为第二个参数来调用此方法.

You can call this method by providing a list (or tuple) of floats as a second parameter.

MyClass.ExternalFunction(myName, [0.0]);

将在2个虚拟机中的2个不同数据结构之间进行转换. Python.Net将在.Net环境中将python的float列表转换为double数组,并将其通过引用传递给您的函数.对第二个参数所做的更改不会传播回python.

There will be a conversion between 2 different data structures in 2 virtual machines. Python.Net will convert python's list of floats to an array of doubles in .Net environment and pass it by reference to your function. Changes made to second parameter will not be propagated back to python.

通过反射,您可以更好地控制参数封送处理.

By using reflection you can get more control over parameters marshaling.

import clr
clr.AddReference("MyDll")
import System
import MyLib

myClassType = System.Type.GetType("MyLib.MyClass, MyDll") 
method = myClassType.GetMethod("ExternalFunction")
numbersArray = System.Array[System.Double]([1.0, 2.0, 3.0])
parameters = System.Array[System.Object](["Benjamin",numbersArray])
method.Invoke(None,parameters)


numbersArray = parameters[1] # an updated Arg2 can be retrieved from parameters array

这篇关于Python .net框架参考参数Double []&的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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