2

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?

2 Answers 2

2

You could use:

Array array = (Array) Params[0];

and then iterate over it using the members of Array, or even using foreach (which will box each element).

The reason it's currently not working is that an array of value type values isn't an array of references - so this wouldn't even compile:

// Invalid
object[] array = new int[10];
Sign up to request clarification or add additional context in comments.

Comments

1

Did you try TestAssembly.WHATEVER w0 = (TestAssembly.WHATEVER)Params[0][0]?

5 Comments

using the exact statement TestAssembly.WHATEVER would mean that I have to add the assembly as a reference to my project and not handle it dynamically.
@dcg: Then just leave it as an object: object w0 = Params[0][0]; or in general object wi = Params[0][i];
agree, that's what it worked for me. But then, how can I iterate through all the items in w0?
@You lost me there. objResults is the first element of Params which you want to iterate through. objResults is already an array, you dont need to cast it: the exception is telling you that object[] cannot be cast to WHATEVER[]. If objResults is an array, to access any member do objResults[i] which is the same as doing directly Params[0][i]. Maybe I'm not understanding you.
Well, question was answered. See below. The problem with your solution is Params is an 1-sized array of objects. The fact that I have added an array for the first element, is a coincidence. To work Params[0][0] should have been declared object [][].

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.