C#如何用函数名字符串来调用函数?除了委托以外,还有什么办法没有?

解决方案 »

  1.   

    反射我晓得
    就没有现成的函数么
    Process(“name”)这么简单的
      

  2.   


     //加载程序集
                Assembly s = Assembly.Load("RecleTest");
                Type tpe = s.GetType("RecleTest.Car");            //调用GetName方法
                MethodInfo method = tpe.GetMethod("GetName");            //获取需要传入的参数
                ParameterInfo[] parms = method.GetParameters();            //这里是判断参数类型
                foreach (ParameterInfo ss in parms)
                {
                    if (ss.ParameterType == typeof(string))
                    {
                        Console.WriteLine("Yes");
                    }
                }            //获取Car对象
                object obj = s.CreateInstance("RecleTest.Car");            //如果有返回值接收下
                method.Invoke(obj, new object[] { "小小" });            Console.ReadLine();
      

  3.   

    string returnValue1 = classSampleType.InvokeMember("ClassMethod", BindingFlags.InvokeMethod | BindingFlags.Public | BindingFlags.Instance, null, null, new object[] { }) as string;
    MethodInfo method0 = (MethodInfo)(new StackTrace().GetFrame(0).GetMethod());
    http://topic.csdn.net/u/20090408/17/3925e17c-241c-401d-a9f2-1eb834cff12f.html