简体   繁体   English

如何使用反射获得重载的私有/受保护方法

[英]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. 我想调用两个Getmethod()重载方法。 If i give the method name , an runtime error is thrown(ambigous method call) . 如果我给出方法名称,则抛出运行时错误(ambigous方法调用)。 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: 您必须准确知道您确切要调用哪一个,并传递Type[] ,例如:

// 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]);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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