Note that there are some explanatory texts on larger screens.

plurals
  1. POReading a struct array encapsulated in an Object in c#
    primarykey
    data
    text
    <p>I am using reflection to handle an assembly loaded at runtime. My problem is that one of the methods has an output parameter which contains an array of structs.</p> <p>Here are the declarations from assembly:</p> <pre><code>public struct WHATEVER { } public class SOMECLASS { public static int methodCall(out WHATEVER[] ppWhateverStructs); } </code></pre> <p>And here's how I tried to execute:</p> <pre><code>Type tWHATEVER = Assembly.Load("path-to-Assembly").GetType("WHATEVER"); Type tSOMECLASS = Assembly.Load("path-to-Assembly").GetType("SOMECLASS"); Array objStructs = Array.CreateInstance(tWHATEVER, 1); object[] Params = new object[] { @objStructs }; // tried with and without "@" - same thing MethodInfo method = tSOMECLASS.GetMethod("methodCall", new Type[] { tWHATEVER.MakeArrayType().MakeByRefType() }); retVal = method.Invoke(null, Params); </code></pre> <p>when I put 'Params' on watch window it shows me that it contains a 1-element array which also contains an N-sized array filled with elements, and objStructs is unchanged. This is correct. My problem is I don't know how to pick items from sub-array:</p> <pre><code>object objRestuls = Params[0]; </code></pre> <p>This statement works, shows the items I expect in watch-window, but I don't know how to iterate and pick them up from object. When I try this:</p> <pre><code>object [] objRestuls = (object [])Params[0]; </code></pre> <p>The following exception is thrown:</p> <pre><code>An unhandled exception of type 'System.InvalidCastException' occurred in TestAssembly.dll Additional information: Unable to cast object of type 'TestAssembly.WHATEVER[]' to type 'System.Object[]'. </code></pre> <p>Does anyone have a hint on how to read an struct-array encapsulated in an object?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload