簡體   English   中英

非零索引數組

[英]Non-zero indexed array

我創建了一個由LabVIEW編譯的.NET庫,它有一個帶有數字和被乘數的函數。 此函數將返回一個數組,其中每個數字都與被乘數相乘。 當在C#中調用該函數時,事實證明該函數采用非零索引數組( double[*] )和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();

在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();

內部異常消息

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附帶的程序InitMarshalArrayIn()函數將double[*]類型編組為double[]

.NET確實支持非零索引的數組。 C#沒有編寫這種類型的語法 ,但您可以使用Array.CreateInstance創建它。

接下來, 使用反射來調用 LabVIEWExports.Multiply並傳遞您創建的數組。

我不確定我是否應該把它作為答案,但在這里它是:

碰巧這是與Visual Studio 2015相關的問題,因為我正在使用Visual Studio Community 2015 Update 1.有關詳細信息,請參閱此內容內容。

如果你使用dynamic (而不是求助於反射),它會工作嗎,如下一行:

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);

變量numbers具有編譯時類型Array to C#,因為C#沒有double[*]的語法。 但也許在dynamic下,運行時的實際numbers類型應該是正確的,這可能是正確的,所以也許后期綁定到正確的方法會有效嗎?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM