将方法名作为参数 如public void getmethod(方法名)对于方法重载的情况下 怎么办 加入重载的方法一个里面有参数 一个里面没有参数这是就没有办法识别调用的到底是哪个方法了! 怎么解决呀 
我有一个类中 定义了两个重载方法 一个里面有参数 一个没有参数现在在另一个类中定义一个方法 参数是方法名 也就是调用这个方法 就可以调用类中的方法对于一般的方法调用没有问题但是对于方法重载 就没有办法识别 请问怎样才能调用成功呀?

解决方案 »

  1.   

    这个问题可以用委托解决。namespace test
    {
        public delegate void StrDelegate(string i);
        public delegate void IntDelegate(int j);    //你所说的方法所在的类
        public partial class A
        {
            public static void Function(string str)
            {
                Console.WriteLine("这是重载方法1");
            }        public static void Function(int i)
            {
                Console.WriteLine("这是重载方法2");
            }        static void Main(string[] args)
            {
                B b = new B();
                b.test(new StrDelegate(Function));
                b.test(new IntDelegate(Function));
            }
        }    //要调用那两个方法的类
        public class B
        {
            public void test(StrDelegate str)
            {
                str("");
            }
            public void test(IntDelegate i)
            {
                i(1);
            }
        }
    }
      

  2.   

    现在在另一个类中定义一个方法 参数是方法名 也就是调用这个方法 就可以调用类中的方法
    --------------------------------------------------------------
    用委托 delegate如果不是一个程序集, 可以用反射解决
      

  3.   

    重载的方法是不能用这样的方式得到的也就是说初了方法名之外,一定要有方法的签名对于重载的方法,lz也许就需要重载你的这个
    "现在在另一个类中定义一个方法 参数是方法名 也就是调用这个方法 就可以调用类中的方法
    "了,hoho用这个吧GetMethod(myMethod, new Type[]{System.Int32, System.String});
    GetMethod(myMethod, new Type[]{System.Int32});
      

  4.   

    不好意思,写错了GetMethod(myMethod, new Type[]{typeof(int), typeof(string)});
    GetMethod(myMethod, new Type[]{typeof(int)});
      

  5.   

    我认为还是用delegate好点,用反射有点大才小用的感觉。