根据类名字符串实例化类,并调用类的方法或函数 转

//获取类型信息

//如果调用其他的DLL 

//System.Reflection.Assembly asmb = System.Reflection.Assembly.LoadFrom("DLL名");

// Type t = asmb.GetType("类名");

//如果是不调用其他DLL

System.Type t = System.Type.GetType("类名"); 

            try

            {

                object dObj = Activator.CreateInstance(t);

                

            //获取方法的信息

                System.Reflection.MethodInfo method = t.GetMethod("方法名");

            //调用方法的一些标志位,这里的含义是Public并且是实例方法,这也是默认的值

            System.Reflection.BindingFlags flag = System.Reflection.BindingFlags.Public | 

 

System.Reflection.BindingFlags.Instance;

            //GetValue方法的参数

            object[] parameters = new object[] { "参数1" };

            //object returnValue = method.Invoke(dObj, flag, Type.DefaultBinder, 

 

parameters, null);

//取得方法返回的值

            object returnValue = method.Invoke(dObj, flag, Type.DefaultBinder, 

 

parameters, null);

            

            }

            catch(Exception ex)

            {

 

            }



下面是我自己写的一个Reflection类,可以根据类名,方法名执行方法
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Text;  
  4. using System.Reflection;  
  5.   
  6. namespace SearchFromDB  
  7. {  
  8.     class Reflection  
  9.     {  
  10.         string ClassName="";  
  11.         Type clstype;  
  12.         public Reflection(string ClassName)  
  13.         {  
  14.             this.ClassName = ClassName;  
  15.         }  
  16.         public Reflection()  
  17.         {  
  18.               
  19.         }  
  20.         /// <summary>  
  21.         /// 实例对象时需要指定类名  
  22.         /// </summary>  
  23.         public object GetClassInstance(string assembly ,string NameSpace)  
  24.         {  
  25.             //assembly为程序集名称,NameSpace为命名空间  
  26.               
  27.             clstype = Assembly.Load(assembly).GetType(string.Concat(NameSpace, "."this.ClassName));  
  28.             if (clstype == null)  
  29.                 return null;  
  30.             object obj = (object)Activator.CreateInstance(clstype);  
  31.             return obj;  
  32.         }  
  33.         /// <summary>  
  34.         /// 实例对象时不用指定类名  
  35.         /// </summary>  
  36.         public object GetClassInstance(string assembly,string NameSpace,string classname)  
  37.         {  
  38.             ClassName = classname;  
  39.             clstype = Assembly.Load(assembly).GetType(string.Concat(NameSpace, ".", classname));  
  40.             if (clstype == null)  
  41.                 return null;  
  42.             object obj = (object)Activator.CreateInstance(clstype);  
  43.             return obj;  
  44.         }  
  45.         /// <summary>  
  46.         /// 执行类的静态方法  
  47.         /// </summary>  
  48.         /// <param name="methodname">  
  49.         /// 类的方法名  
  50.         /// </param>  
  51.         /// <param name="parameters">  
  52.         /// 方法的参数类型  
  53.         /// </param>  
  54.         /// <param name="methodtype">  
  55.         /// 方法的参数  
  56.         /// </param>  
  57.   
  58.         public object GetMethod(string methodname, Type[] methodtype, object[] parameters)  
  59.         {  
  60.             // methodtype.SetValue(typeof(string),1);  
  61.             System.Reflection.MethodInfo pMethod = clstype.GetMethod(methodname, BindingFlags.IgnoreCase | BindingFlags.Static | BindingFlags.Public, null, methodtype, null);  
  62.             //调用方法的一些标志位,这里的含义是Public并且是实例方法,这也是默认的值  
  63.             //System.Reflection.BindingFlags flag = BindingFlags.IgnoreCase | BindingFlags.Static | BindingFlags.Public;  
  64.             object returnValue = pMethod.Invoke(null, parameters);  
  65.             //string returnValue = pMethod.Invoke(clsObj, flag, Type.DefaultBinder, parameters,null).ToString();  
  66.             return returnValue;  
  67.         }  
  68.     }  

posted on 2012-04-18 08:33  武胜-阿伟  阅读(3206)  评论(0编辑  收藏  举报