C# 非零索引数组

C# 非零索引数组,c#,.net,arrays,visual-studio-2015,C#,.net,Arrays,Visual Studio 2015,我创建了一个由LabVIEW编译的.NET库,它有一个接受数字数组和被乘数的函数。此函数将返回一个数组,其中每个数字都与被乘数相乘。在C#中调用该函数时,结果表明该函数使用非零索引数组(double[*])和int作为参数,并返回另一个非零索引数组 我能够使用C#的array.CreateInstance()方法创建非零索引数组。但是,我无法将此数组传递到函数中,因为所需的数据类型是double[*] 从互联网上的研究来看,.NET似乎不支持非零索引数组类型。我试图找到一种方法来修改LabVIE

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

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

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

有没有关于如何解决这个问题的建议

更新1

LabVIEW框图

C#程序

内部异常消息

无法将“System.Double[*]”类型的对象强制转换为“System.Double[]”类型。

内部异常堆栈跟踪

位于NationalInstruments.LabVIEW.Interop.DataMarshall.InitMarshalArrayIn(IntPtr数据,数组,Marshal1DArray val)
在LabVIEW.Interop.LabVIEWInteropExports.MultiplyArray(双[*]输入数组,Int32数字)


似乎在执行的某个时刻,程序尝试使用LabVIEW附带的程序集中的
InitMarshalArrayIn()
函数将类型
double[*]
封送到
double[]

。NET支持非零索引数组C#没有语法来编写这样的类型,但是您可以使用
Array.CreateInstance
创建它


接下来,使用反射调用
LabVIEWExports。将
相乘并传递您创建的数组。

我不确定是否应将此作为答案,但它是这样的:


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

如果您使用
动态
(而不是诉诸反射),它会起作用吗,如下面的最后一行:

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.
var 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 = (Array)LabVIEWExports.Multiply((dynamic)numbers, 2);
const int Length=5;
常量int LowerBound=1;
//实例化非零索引数组。该数组是一维的,并且
//大小由长度指定,下限由LowerBond指定。
var numbers=Array.CreateInstance(typeof(double)、newint[]{Length}、newint[]{LowerBound});
//初始化数组。

对于(int i=numbers.GetLowerBound(0);i@Nishat:请不要对非代码使用代码块….NET支持非零索引的数组。C#没有语法来编写这样的类型,但您可以使用数组创建它。创建*并使用反射传递它。您是指double*[]吗?double[*]是什么意思?检查参数ElementType您可以执行typeof(yourRequiredType)@mathusmut,
double[*]
是类型为
double
的非零索引数组。尝试使用反射调用
LabVIEWExports。乘法
(请参阅问题中的更新2),但遇到异常。我不太确定我是做错了,还是仅仅因为LabVIEW本身不支持从
double[*]
double[]的转换
?对我不起作用。它在运行时抛出
System.InvalidCastException
。@NoctisTong OK。我没有VS2015及其新的C#编译器,我也没有CLR版本4.6,但可能与使用反射时看到的问题相同(问题中的更新2)?从您链接的页面中,我得到的印象是,它曾经与VS2013等一起工作。@Jeppee,我尝试调用由VS 2010生成的LabVIEW生成的相同程序集,它可以工作。它似乎是VS问题,而不是LabVIEW问题。@NoctisTong它是在同一台机器上吗?我问这个问题是因为很有兴趣知道它是否与IL有关由C#编译器生成,或CLR(运行时)出现问题。在第一种情况下,可能是C#编译器输出了一些错误的IL代码,强制检查
double[*]
(也称为
double[…]
)无法通过。在第二种情况下,它可能是版本4.6运行时中的一个错误。您是否尝试过在Visual Studio 2015中将C#target版本更改为C#5.0,如在中?@NoctisTong我的假设是,这是VS2015的C#编译器中的一个错误,即使我们针对早期的C#版本,它也不会消失(并且仍然使用VS2015 C#编译器)。您可以打开
ildasm
并打开VS2015生成的EXE/DLL,浏览到有问题的方法,并阅读IL,看看它是否提到
float64[]
或类似内容(这将崩溃,因为实例不是“向量”
float64[]
,而是一个“秩一的多维数组”
float64[…]
)。
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();
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.
var 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 = (Array)LabVIEWExports.Multiply((dynamic)numbers, 2);