IronPython的:如何调用需要的价值类型的数组功能? [英] IronPython: How to call a function that expects an array of value-types?

查看:513
本文介绍了IronPython的:如何调用需要的价值类型的数组功能?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我所遇到的与IronPython的一个问题,我解决不了。我需要调用一个函数,数组类型的参数值类型。函数签名(在C ++ / CLI表示法)是这样的:

 静态INT PLGServiceInterface :: PLGService :: MapLowSpeed​​RealtimeNames(CLI ::数组<系统::字符串^> ^ SignalNames,诠释timeout_ms,CLI ::阵列< PLGServiceInterface :: LVCluster_1> ^%频道,系统:字符串^%ReportText)

当我打电话从IronPython的功能

 进口CLR
clr.AddReferenceToFileAndPath('PLGServiceAPI.dll')从系统导入字符串数组
从PLGServiceInterface进口PLGService,LVCluster_1输出文本= clr.Reference [字符串]()
outdata = clr.Reference [数组[LVCluster_1]()
PLGService.MapLowSpeed​​RealtimeNames(('你好','世界'),300,outdata,输出文本)

我收到以下错误

 回溯(最后最近一次调用):
  文件test2.py,9号线,上述<&模块GT;
类型错误:预期的StrongBox [数组[LVCluster_1],得到的StrongBox [数组[LVCluster_1]

该错误信息是不是非常有帮助,但我认为问题在于outdata是包含值类型,而不是引用类型的数组。显然,IronPython的不知道该怎么办拳击在这种情况下。

使用C ++ / CLI我可以使用的功能就好了:

 使用命名空间系统;
使用PLGServiceInterface :: LVCluster_1;
使用PLGServiceInterface :: PLGService;INT主(阵列<系统::字符串^> ^参数)
{
    阵列< LVCluster_1> ^ outdata;
    阵列<弦乐^> ^名字= gcnew阵列<弦乐^> {一,二};
    字符串^ O;    PLGService :: MapLowSpeed​​RealtimeNames(名称,300,outdata,O);    控制台:的WriteLine(O);    控制台:阅读();    返回0;
}

我想如果该函数将代替期望引用数组

 阵列< LVCluster_1 ^> ^ outdata

我可以和IronPython的调用它。

有没有什么办法让与IronPython的这项工作?顺便说一句,装配与LabView的创建和LVCluster_1是LabVIEW的集群(结构)。

编辑:
MapLowSpeed​​RealtimeNames的中间语言的签名是这样的:

 。方法公开hidebysig静态INT32 MapLowSpeed​​RealtimeNames(字符串[...] SignalNames,
                                                                INT32 timeout_ms,
                                                                [出]值类型PLGServiceInterface.LVCluster_1 [...]&安培;频道,
                                                                [出]串放; ReportText)CIL管理

是否有人知道3个点的数组括号中的含义是什么?当我编译一个函数,需要一个数组中的C ++ / CLI,我只得到没有点开闭括号之间。

这点似乎可疑我,因为我在调用的时候也得到了类型错误
方法采用双(float64)的类型的数组的out参数:

 。方法公开hidebysig静态INT32 ReadVariables(字符串[...] SignalNames,
                                                     INT32 timeout_ms,
                                                     [出]串放; ReportText,
                                                     [出] float64 [...]&安培;数据)CIL管理

生成错误

 类型错误:预期的StrongBox [数组[浮动],得到的StrongBox [数组[浮动]


解决方案

您是否尝试过使用Array.CreateInstance(LVCluster_1,长度)来创建阵列?见下面的例子:

  outdata = Array.CreateInstance(LVCluster_1,2)

在code以上为我的作品,但我的C ++ / CLI函数签名不使用跟踪引用(%)。所以,如果我要重新编写函数签名,它看起来像:

 静态INT PLGServiceInterface :: PLGService :: MapLowSpeed​​RealtimeNames(CLI ::数组<系统::字符串^> ^ SignalNames,诠释timeout_ms,CLI ::阵列< PLGServiceInterface :: LVCluster_1> ^频道,系统:字符串^ ReportText)

I have come across a problem with IronPython that I can't solve. I need to call a function that takes a parameter of type array to value-type. The function-signature (in C++/CLI notation) is this:

static int PLGServiceInterface::PLGService::MapLowSpeedRealtimeNames(cli::array<System::String ^> ^ SignalNames, int timeout_ms, cli::array<PLGServiceInterface::LVCluster_1> ^% Channels, System::String ^% ReportText)

When I call the function from IronPython

import clr
clr.AddReferenceToFileAndPath('PLGServiceAPI.dll')

from System import String, Array
from PLGServiceInterface import PLGService, LVCluster_1

outtext = clr.Reference[String]()
outdata = clr.Reference[Array[LVCluster_1]]()
PLGService.MapLowSpeedRealtimeNames(('hello', 'world'), 300, outdata, outtext)

I get the following error

Traceback (most recent call last):
  File "test2.py", line 9, in <module>
TypeError: expected StrongBox[Array[LVCluster_1]], got StrongBox[Array[LVCluster_1]]

The error message is not very helpful but I assume the problem is that "outdata" is an array containing value types instead of reference types. Apparently IronPython doesn't know how to do the boxing in that case.

With C++/CLI I can use the function just fine:

using namespace System;
using PLGServiceInterface::LVCluster_1;
using PLGServiceInterface::PLGService;

int main(array<System::String ^> ^args)
{
    array<LVCluster_1> ^outdata;
    array<String^> ^names = gcnew array<String^>{"one", "two"};
    String ^o;

    PLGService::MapLowSpeedRealtimeNames(names, 300, outdata, o);

    Console::WriteLine(o);

    Console::Read();

    return 0;
}

I assume if the function would instead expect an array of references

array<LVCluster_1 ^> ^outdata

I could call it with IronPython.

Is there any way to make this work with IronPython? By the way, the assembly was created with LabView and LVCluster_1 is a LabView-Cluster (structure).

Edit: The Intermediate Language signature of MapLowSpeedRealtimeNames is this:

.method public hidebysig static int32  MapLowSpeedRealtimeNames(string[...] SignalNames,
                                                                int32 timeout_ms,
                                                                [out] valuetype PLGServiceInterface.LVCluster_1[...]& Channels,
                                                                [out] string& ReportText) cil managed

Does anybody know what the meaning of the 3 dots in the array brackets is? When I compile a function that takes an array in C++/CLI, I only get the opening and closing brackets without the dots in between.

These dots seem suspicious to me because I also get the TypeError when calling a method that takes an out-parameter of type array of double(float64):

.method public hidebysig static int32  ReadVariables(string[...] SignalNames,
                                                     int32 timeout_ms,
                                                     [out] string& ReportText,
                                                     [out] float64[...]& Data) cil managed

Generates the error

TypeError: expected StrongBox[Array[float]], got StrongBox[Array[float]]

解决方案

Have you tried using Array.CreateInstance(LVCluster_1, length) to create the array? See example below:

outdata = Array.CreateInstance(LVCluster_1, 2)

The code above works for me, but my c++/cli function signature don't use the tracking reference (%). So if I were to re-write your function signature it would look like:

static int PLGServiceInterface::PLGService::MapLowSpeedRealtimeNames(cli::array<System::String ^> ^ SignalNames, int timeout_ms, cli::array<PLGServiceInterface::LVCluster_1> ^ Channels, System::String ^ ReportText)

这篇关于IronPython的:如何调用需要的价值类型的数组功能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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