Zero Index Array

I created the .NET library compiled by LabVIEW, which has a function that accepts an array of numbers and animation. This function will return an array in which each number is multiplied by the multiplicand. When a function is called in C #, it turns out that the function takes a nonzero indexed array ( double[*]) and intas parameters and returns another nonzero indexed array.

I can create a nonzero indexed array using the C # method Array.CreateInstance(). However, I cannot pass this array to a function since a data type is required double[*].

From Internet research, it seems that .NET does not support a non-zero indexed array type. I tried to find a way to modify the LabVIEW program to generate a function that takes a zero index without using.

Any tips on how I can get around this?

Update 1

LabVIEW Flowchart LabVIEW Flowchart

C # program

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

LabVIEW Function Signature in C #

enter image description here

Update 2

Tried to use C # Reflection to call the LabVIEW function with the following codes, but to meet 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();

Internal exception message

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

Internal exception stack trace

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

It seems that at some point in the execution, the program tries to marshal the type double[*]on double[]using the function InitMarshalArrayIn()in the assembly that comes with LabVIEW.

+4
3

.NET , . # , , Array.CreateInstance.

LabVIEWExports.Multiply .

+3

, , :

, , Visual Studio 2015, Visual Studio Community 2015. 1. . this this .

+3

, 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 #, # double[*]. , , dynamic, numbers , , ?

+1
source

Source: https://habr.com/ru/post/1629334/


All Articles