3

I'm having trouble grasping reflection in C#, so I'm going to put my specific situation down and see what you guys can come up with. I've read TONS of C# reflection questions on here, but I still just simply don't get it.

So here's my situation; I'm trying to access an array which is a non-public member of a class I have access to.

alt text

Basically it's a System.Collections.CollectionBase which has an array variable called "list", but it has this parent type of OrderCollection and the reflection of it is just confusing the hell out of me.

I have to do a lot of these so a good guide or example would really help. Please let me know if you would like more information.

I blacked out the name of the namespace not because what I'm doing is not illegal by any means, but I'm trying to be first to market on this and so I'm trying to be careful.

1
  • 2
    Don't use magic to drive a car. A.k.a. don't make it more complex than it is. Reflection is complex. Good design is better. Commented Sep 23, 2010 at 13:45

2 Answers 2

9

What are you trying to use reflection at all? CollectionBase supports indexing, but only through the explicit interface implementation of IList, so you ought to be able to write:

IList list = Acct.Orders; response = list[0]; 

You may need to cast the result to a more appropriate type, but I don't see any need for reflection here.

EDIT: Original answer didn't take account of explicit interface implementation.

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

2 Comments

Jon, I've seen you answer just about every reflection question I've looked up here on StackOverflow. And here you come to the rescue. To answer your question, I don't know why I thought I needed that, guess I just figured it should be harder. I'm kind of new to C# so I'm lost out here so-to-speak. Nevertheless, you're absolutely correct no need for reflection.
@Zach: No problem. "You don't need to use reflection" is about the most pleasant answer I can ever give to a reflection question - it's nice to have it available, but nicer when you don't need it...
0

While this may not help you, it may help others. Here is a simplified example of reflection:

using System; using System.Reflection; namespace TeamActivity { class Program { static void Main(string[] args) { // Dynamically load assembly from the provided DLL file. Assembly CustomerAssembly = Assembly.LoadFrom( "BasicCalculations.dll" ); // Get a Type from the Assembly Type runtimeType = CustomerAssembly.GetType( "BasicCalcuation.BasicCalc" ); // Get all methods from the Type. MethodInfo[] methods = runtimeType.GetMethods(); // Loop through all discovered methods. foreach ( MethodInfo method in methods ) { Console.WriteLine( "Method name: " + method.Name ); // Create an array of parameters from this method. ParameterInfo[] parameters = method.GetParameters(); // Loop through every parameter. foreach ( ParameterInfo paramInfo in parameters ) { Console.WriteLine( "\tParamter name: " + paramInfo.Name ); Console.WriteLine( "\tParamter type: " + paramInfo.ParameterType ); } Console.WriteLine( "\tMethod return parameter: " + method.ReturnParameter ); Console.WriteLine( "\tMethod return type: " + method.ReturnType ); Console.WriteLine("\n"); } // Invoke the Type that we got from the DLL. object Tobj = Activator.CreateInstance( runtimeType ); // Create an array of numbers to pass to a method from that invokation. object[] inNums = new object[] { 2, 4 }; // Invoke the 'Add' method from that Type invokation and store the return value. int outNum = (int)runtimeType.InvokeMember( "Add", BindingFlags.InvokeMethod, null, Tobj, inNums ); // Display the return value. Console.WriteLine( "Output from 'Add': " + outNum ); Console.WriteLine( "\nPress any key to exit." ); Console.ReadKey(); } } } 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.