非零索引数组

Noc*_*ong 5 .net c# arrays visual-studio-2015

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

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

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

关于我如何解决这个问题的任何建议?

更新1

LabVIEW程序框图 LabVIEW程序框图

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();
Run Code Online (Sandbox Code Playgroud)

在C#中签名LabVIEW函数

在此输入图像描述

更新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();
Run Code Online (Sandbox Code Playgroud)

内部异常消息

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程序集.

Noc*_*ong 3

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

碰巧这是与 Visual Studio 2015 相关的问题,因为我正在使用 Visual Studio Community 2015 Update 1。有关更多信息,请参阅此。