C # Reflection How-to?

I am having trouble understanding reflection in C #, so I'm going to pose my specific situation and see what you guys can come up with. I read TONS questions about C # reflection here, but I still just don't get it.

So, here is my situation; I am trying to access an array that is a non-public member of a class that I have access to.

alt text

Basically, this is System.Collections.CollectionBase , which has an array variable called "list" but has this parent type OrderCollection , and reflecting it just baffles me.

I need to make a lot of them, so a good reference or example will really help. Please let me know if you need more information.

I crossed out the namespace name not because what I am doing is not illegal, but I try to be the first in this market, so I try to be careful.

+3
source share
2 answers

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

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

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

EDIT: The original answer did not account for the explicit implementation of the interface.

+9
source

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

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

Source: https://habr.com/ru/post/1766224/


All Articles