Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't access/convert System.Double[*] object

I use an interface to access an old application. From this application I have some "double array" which I can't access.The return type is declared as virtual dynamic. Whenever I access the array I got an exception.

On this question I found out that it may be because of the wrong indexed array. So I've tried the proposed solution but as I said I could not even access the array once without getting the exception.

Any idea on what the error could be?

Code with Hans approach:

var dataSetValues = dataSet.DoubleArray;
var result = ConvertDoubleArray(dataSetValues); // <<<<<< This is where I get an exception

public static double[] ConvertDoubleArray(Array arr)
{
    if (arr.Rank != 1) 
        throw new ArgumentException();

    var retval = new double[arr.GetLength(0)];
    for (int ix = arr.GetLowerBound(0); ix <= arr.GetUpperBound(0); ++ix)
        retval[ix - arr.GetLowerBound(0)] = (double) arr.GetValue(ix);
    return retval;
}

Interface declaration of DoubleArray:

public virtual dynamic DoubleArray { get; set; }

Runtime type information: enter image description here

Exception:

System.InvalidCastException: Unable to cast object of type 'System.Double[*]' to type 'System.Double[]'. at CallSite.Target(Closure , CallSite , VirtualEnvironmentManager , Object ) at System.Dynamic.UpdateDelegates.UpdateAndExecute2[T0,T1,TRet](CallSite site, T0 arg0, T1 arg1) at FormulaTestExecutor.Common.VirtualEnvironmentManager.CreateVirtualStructure(Formula formula) in D:\Develop\TFS\Main\tools\FormulaMTest\FormulaTestExecutor\FormulaTestExecutor\Common\VirtualEnvironmentManager.cs:line 55

Stacktrace of Exception:

FormulaTestExecutor.exe!FormulaTestExecutor.Common.VirtualEnvironmentManager.CreateVirtualStructure(FormulaTestExecutor.Model.Formula formula = {FormulaTestExecutor.Model.Formula}) Line 55 C# Symbols loaded. FormulaTestExecutor.exe!FormulaTestExecutor.Program.Main(string[] args = {string[0]}) Line 18 C# Symbols loaded.

like image 960
Hendrik Avatar asked Jul 25 '26 05:07

Hendrik


1 Answers

You must do this:

var dataSetValues = dataSet.DoubleArray; // dataSetValues is dynamic
var result = ConvertDoubleArray((Array)(object)dataSetValues);

The reason is the 'dynamic' type of the DoubleArray (that was probably automatically defined in the interface when you added the COM reference). It's a super smart thing that tries to do the conversion from System.Double[*] to System.Double[] by itself, but it's not smart enough to do this (it can't read StackOverflow answers... yet)

So, you'll have to ask it to just pass the object 'as is', to be able to pass it directly to the CLR low-level cast, which can do System.Double[*] to Array w/o crashing. Once you have the Array, you can re-use the ConvertDoubleArray utility.

like image 152
Simon Mourier Avatar answered Jul 27 '26 17:07

Simon Mourier



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!