简体   繁体   中英

HOW TO get an overloaded private/protected method using reflection

using System;
using System.Reflection;

namespace Reflection        
{
    class Test
    {
        protected void methodname(int i)
        {
            Console.WriteLine(("in the world of the reflection- only i"));
            Console.Read();
        }    
        protected void methodname(int i, int j)
        {
            Console.WriteLine(("in the world of the reflection  i , j"));
            Console.Read();
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
           // BindingFlags eFlags = BindingFlags.Default | BindingFlags.Instance | BindingFlags.Public|BindingFlags.NonPublic;
            BindingFlags eFlags = BindingFlags.Instance|BindingFlags.NonPublic;
            Test aTest = new Test();
            MethodInfo mInfoMethod = typeof(Reflection.Test).GetMethod("methodname", eFlags);
            mInfoMethod.Invoke(aTest, new object[] { 10 ,20});   
        }
    }
}

I want to call both Getmethod() overloaded methods. If i give the method name , an runtime error is thrown(ambigous method call) . How to avoid this and how each method can be called.

You have to pass types of your overloaded method, this is how reflection sorts out your desired method when there's a overload.

You can't call both the methods as it has different types of input parameter. You have to know exactly which one you exactly want to call, and pass along a Type[] , for instance:

// invoking overload with two parameters
MethodInfo mInfoMethod =
    typeof(Reflection.Test).GetMethod(
        "methodname",
        BindingFlags.Instance | BindingFlags.NonPublic,
        Type.DefaultBinder,
        new[] {typeof (int), typeof (int)},
        null);

mInfoMethod.Invoke(aTest, new object[] { 10 ,20});

OR

// invoking overload with one parameters
MethodInfo mInfoMethod =
    typeof(Reflection.Test).GetMethod(
        "methodname",
        vBindingFlags.Instance | BindingFlags.NonPublic,
        Type.DefaultBinder,
        new[] { typeof (int) },
        null);

mInfoMethod.Invoke(aTest, new object[] { 10 });

使用'GetMethods'来检索所有重载,然后选择你想要的重载。

Please find a working sample below:

public class ReflectionSample
    {
        protected void Method(int i)
        {
            Console.WriteLine(string.Format("in the world of the reflection- only {0}", i));
            Console.Read();
        }
        protected void Method(int i, int j)
        {
            Console.WriteLine(string.Format("in the world of the reflection  {0} , {1}", i,j));
            Console.Read();
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            var eFlags = BindingFlags.Instance | BindingFlags.NonPublic;
            var objType = Type.GetType("Sample.ReflectionSample");
            var methods = objType.GetMethods(eFlags);
            foreach (var method in methods)
            {
                if (method.Name == "Method")
                {
                    Console.WriteLine("Method name is :" + method.Name);
                    var parameters = method.GetParameters();
                    int value = 10;
                    List<object> param = new List<object>();
                    for (int i = 0; i < parameters.Count(); i++)
                    {
                        param.Add(value * 5);
                    }
                    Console.WriteLine(parameters.Count());
                    method.Invoke(new ReflectionSample(), param.ToArray());
                }
            }
        }
    }

can u try like this

You have to specify which method you want:

class SomeType 
{
    void Foo(int size, string bar) { }
    void Foo() { }
}

SomeType obj = new SomeType();
// call with int and string arguments
obj.GetType().GetMethod("Foo", new Type[] { typeof(int), typeof(string)).Invoke(obj, new object[] { 42, "Hello" });
// call without arguments
obj.GetType().GetMethod("Foo", new Type[0]).Invoke(obj, new object[0]);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM