zkwentz
zkwentz

Reputation: 1105

C# Reflection How-to?

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.

Upvotes: 3

Views: 669

Answers (2)

Adam Howell
Adam Howell

Reputation: 445

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();
        }
    }
}

Upvotes: 0

Jon Skeet
Jon Skeet

Reputation: 1499790

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.

Upvotes: 9

Related Questions