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.
Here are the declarations from assembly:
public struct WHATEVER
{
}
public class SOMECLASS
{
public static int methodCall(out WHATEVER[] ppWhateverStructs);
}
And here's how I tried to execute:
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);
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:
object objRestuls = Params[0];
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:
object [] objRestuls = (object [])Params[0];
The following exception is thrown:
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[]'.
Does anyone have a hint on how to read an struct-array encapsulated in an object?