2

I am trying to use reflection to create object array of the type created from reflection like the folowing:

Client[] newArray = new Client[] {client1, client2};

I need to somehow get the Client object type to create the object so it can be passed through.

Any help would be greatly appreciated.

Cheers, Rob

object clientObject = testAssembly.CreateInstance(".Testing_Automation.Client");        
Type client = testAssembly.GetType(".Testing_Automation.Client");

// Create Client Object Array

Passing to:

public Appointment(IEnumerable<Client> client, string time)
1
  • It's hard to cast into some type that you don't know at compile time. The IEnumerable<Client> conversion will be the trickier one to perform. Commented Jun 12, 2009 at 11:31

1 Answer 1

6

You should use Array.CreateInstance method:

Array arr = Array.CreateInstance(client, lengthOfArray);
arr.SetValue(client1, 0); // Fill in the array...
arr.SetValue(client2, 1);

To get an IEnumerable<Client> from the array, you could use (IEnumerable<Client>)arr if you know the Client type at compile time. If you don't, which is likely, you should post more info about the possibilites of that method call.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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