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

发布于 2024-12-20 02:42:03 字数 1038 浏览 2 评论 0原文

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

我想调用两个 Getmethod() 重载方法。如果我给出方法名称,则会抛出运行时错误(不明确的方法调用)。如何避免这种情况以及如何调用每个方法。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

荒岛晴空 2024-12-27 02:42:03

您必须传递重载方法的类型,这就是反射在重载时对所需方法进行排序的方式。

您不能同时调用这两个方法,因为它们具有不同类型的输入参数。您必须确切地知道您到底想调用哪个,并传递 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 });

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 });
滥情空心 2024-12-27 02:42:03

使用“GetMethods”来检索所有重载,然后选择您想要的重载。

Use 'GetMethods' instead to retrieve all the overloads, then pick the ones you want.

梦开始←不甜 2024-12-27 02:42:03

请在下面找到一个工作示例:

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

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());
                }
            }
        }
    }
盗琴音 2024-12-27 02:42:03

你可以这样尝试吗

你必须指定你想要哪种方法:

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

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]);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文