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

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

从Internet上的研究来看,.NET似乎不支持非零索引数组类型。我试图找到一种方法来修改LabVIEW程序,以生成不使用零索引数组的函数。

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

更新1

LabVIEW程序框图
c# - 非零索引数组-LMLPHP

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函数的签名

c# - 非零索引数组-LMLPHP

更新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)
似乎在执行的某个时刻,程序尝试使用LabVIEW随附程序集中的double[*]函数将double[]类型编码为InitMarshalArrayIn()

最佳答案

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

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

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

10-12 12:48
查看更多