c# - 非零索引数组

标签 c# .net arrays visual-studio-2015

我创建了一个由 LabVIEW 编译的 .NET 库,它有一个接受数字数组和一个被乘数的函数。此函数将返回一个数组,其中每个数字都已与被乘数相乘。当在 C# 中调用该函数时,事实证明该函数采用非零索引数组 ( double[*] ) 和 int。作为参数并返回另一个非零索引数组。

我能够使用 C# 的 Array.CreateInstance() 创建一个非零索引数组方法。但是我无法将此数组传递给函数,因为所需的数据类型是 double[*] .

从互联网上的研究来看,.NET 似乎不支持非零索引数组类型。我试图找到一种方法来修改 LabVIEW 程序以生成采用零索引数组但无济于事的函数。

关于如何解决这个问题有什么建议吗?

更新 1

LabVIEW框图 LabVIEW block diagram

C#程序

const int Length = 5;
const int LowerBound = 1;
// Instanstiate a non-zero indexed array. The array is one-dimensional and
// has size specified by Length and lower bound specified by LowerBound.
Array numbers = Array.CreateInstance(typeof(double), new int[] { Length }, new int[] { LowerBound });
// Initialize the array.
for (int i = numbers.GetLowerBound(0); i <= numbers.GetUpperBound(0); i++)
{
    numbers.SetValue(i, i);
}

var variable = LabVIEWExports.Multiply(numbers, 2); // This is invalid as numbers is not typed double[*].
Console.ReadKey();

C# 中 LabVIEW 函数的签名

enter image description here

更新 2

尝试使用C#的Reflection调用LabVIEW函数,代码如下,遇到TargetInvocationException .

const int Length = 5;
const int LowerBound = 1;
const string methodName = "MultiplyArray";
const string path = @"C:\";

Array numbers = Array.CreateInstance(typeof(double), new int[] { Length }, new int[] { LowerBound });
for (int i = numbers.GetLowerBound(0); i <= numbers.GetUpperBound(0); i++)
{
    numbers.SetValue(i, i);
}

Assembly asm = Assembly.LoadFile(path + "LabVIEW.Interop.dll");
Type type = asm.GetType("LabVIEW.Interop.LabVIEWInteropExports");

if (type != null)
{
    MethodInfo methodInfo = type.GetMethod(methodName);

    if (methodInfo != null)
    {
        object result = methodInfo.Invoke(methodInfo, new object[] { array, multiplicand }); // Throw exception.
    }
}
Console.ReadKey();

内部异常消息

Unable to cast object of type 'System.Double[*]' to type 'System.Double[]'.

内部异常堆栈跟踪

at NationalInstruments.LabVIEW.Interop.DataMarshal.InitMarshalArrayIn(IntPtr data, Array array, Marshal1DArray val) at LabVIEW.Interop.LabVIEWInteropExports.MultiplyArray(Double[*] input__32Array, Int32 numeric)

似乎在执行的某个时刻,程序试图编码类型 double[*]double[]InitMarshalArrayIn() LabVIEW 附带的程序集中的函数。

最佳答案

我不确定我是否应该把它作为答案,但这里是:

碰巧这是一个与 Visual Studio 2015 相关的问题,因为我正在使用 Visual Studio Community 2015 Update 1。参见 thisthis获取更多信息。

关于c# - 非零索引数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35477266/

相关文章:

c# - 通过索引获取列表项

c# - AutoResetEvent.Set() 是做什么的?

.net - .NET下的原子文件复制

c# - 接口(interface)可以需要一个属性,但不指定所需的类型吗?

c# - 在 Func/lambda 表达式中重用方法调用

c# - LINQ Parallel.ForEach<> 循环上的 lock 关键字

c++ - 如何在 C++ 中从 bytearray 中提取字节时交换 64 位整数?

arrays - Powershell-调用每个对象的函数会更改所有对象中的数组时的奇怪行为

.net - WiX 安装项目 - 每次编译后停止构建

php - 在 PHP 中从 MySQL 返回 blob 数据并转换为 base64_encode 以添加到数组