请问各位高手,能否有什么方法使得我在只知道类中某些方法的名字时,动态的调用这些方法?
例如: Class A中有 一个名字叫 TestFunc()的方法,我只知道“TestFunc”这个字符串的话,如何去调用这个方法?

解决方案 »

  1.   

    比如有一个类库,包括两个函数,一个带参数,一个不带
    using System;
    using System.Reflection;
    using System.Globalization;namespace Lib
    {
    public class TestLib
    {
            private string _returnString;
            public string ReturnString
            {
                get { return _returnString; }
                set { _returnString = value; }
            } public TestLib()
    {
    Console.WriteLine("Create a new instance");
    }        public TestLib(string str)
            {
                _returnString = str;
            } public void FunctionA(string [] paras)
    {
    try
    {
    string b=null;
    for (int i=0;i<paras.Length;i++)
    b+=paras[i]; Console.WriteLine("A--"+System.DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss:ffff")+"---"+b);
    }
    catch (Exception ex)
    {
    Console.WriteLine("FunctionA: "+ex.ToString());
    }
    } public void FunctionB()
    {
    Console.WriteLine("B--"+System.DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss:ffff"));
    }
    }
    }
      

  2.   

    在程序中引用这个类,然后用反射调用using System;
    using System.Reflection;
    using System.Globalization;
    using Lib;namespace Reflection
    {
    /// <summary>
    /// Summary description for Class1.
    /// </summary>
    class Class1
    {
    /// <summary>
    /// The main entry point for the application.
    /// </summary>
    [STAThread]
    static void Main(string[] args)
    {
                string[] a = new string[3];
                a[0] = "ABCDE";
                a[1] = "DEFGHI";
                a[2] = "3454564675";            Call("FunctionA", a);
                Call("FunctionB", null);
    Console.ReadLine();
    } static void Call(string function_name, string [] paras)
    {
    string b = "Lib.dll";
    Assembly a = Assembly.LoadFrom(b); Type t = a.GetType("Lib.TestLib");//("TestLib");
    try
    {
    //Console.WriteLine(t.FullName);
                    if (paras != null)
                    {
                        MethodInfo m = t.GetMethod(function_name, new Type[] { typeof(string[]) });
                        Object obj = Activator.CreateInstance(t);
                        Console.WriteLine("------------" + obj.GetType());
                        m.Invoke(obj, new object[] { paras });
                    }
                    else
                    {
                        MethodInfo m = t.GetMethod(function_name);
                        Object obj = Activator.CreateInstance(t);
                        Console.WriteLine("------------" + obj.GetType());
                        m.Invoke(obj, null);
                    }
    }
    catch (Exception ex)
    {
    Console.WriteLine("Main: "+ex.ToString());
    }
    }
    }
    }
      

  3.   

    有楼上说的那么麻烦吗,给Class A加个空间,你引入这个这个空间,new一个A的变量,就能用它的方法了
      

  4.   

    再问一下,这里的参数能否是那些复杂的类型,比如SqlTranscation之类的?
      

  5.   

    System.Reflection.MethodBase.GetCurrentMethod().Name
      

  6.   

    namespace Test
    {
        class Program
        {
            static void Main(string[] args)
            {            Console.Write(getname());
                
            }
            static string getname()
            {
                return System.Reflection.MethodBase.GetCurrentMethod().Name;
            }
        }
    }
      

  7.   

    To:wcmj(望尘莫及) ( ) 
    有楼上说的那么麻烦吗,给Class A加个空间,你引入这个这个空间,new一个A的变量,就能用它的方法了
    现在是只有这个名称,不想直接用示例;
    比如一个动态配置运行的文件,在不同的情况下用不同的函数,有时用A函数,有时用B函数,或者还有很多的其他函数,我们不能把函数写死了,那样就要根据不同情况来改程序,如果用反射配置下就可以了To:foyuan(暴走零零漆)
    你的程序是返回调用函数的名称,而LZ的意思是要根据函数名称(字符串)来调用函数。
      

  8.   

    Reflection,反射就可以了~~可以先看看相关知识~~
    这个用来搞抽像工厂很爽的~~
      

  9.   

    wangsaokui(无间道III(终极无间)C#MVP) 的回答很有帮助,我先研究研究
    谢谢
      

  10.   

    再问一下,这里的参数能否是那些复杂的类型,比如SqlTranscation之类的?MethodInfo m = t.GetMethod(function_name, new Type[] { typeof(string[]) });
    这句可以修改为复杂类型,如果是自定义的类型,需要引用类型的定义,否则不知道怎么转
      

  11.   

    又学到知识了,谢谢wangsaokui(无间道III(终极无间)C#MVP)
    呵呵