反射reflect

来源:互联网 发布:mac系统安装教程视频 编辑:程序博客网 时间:2024/06/06 06:22
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Reflection;using System.Xml;using System.Data;namespace AutoService{    public class ReflectionCS    {        public string DoReflectionNoParas(string path, string namesp, string classname, string methodName)        {            string errorMessage = "OK";//返回OK表示方法正常执行            try            {                Assembly ass;                Type type;                Object obj;                Object any = new Object();                //ass = Assembly.LoadFile(@"E:\Service\WindowsService1\Reflection\bin\Debug\Reflection.dll");                ass = Assembly.LoadFile(@"" + path + "");                //Must be Namespace with class name  //获取方法类型,第一个参数是:命名空间+方法名,第二个参数是是否抛出异常,第三个参数是是否区分大小写                type = ass.GetType(namesp + "." + classname, true, false);                //获取方法集合                MethodInfo[] listMethodInfo = type.GetMethods();                //实例化                //obj = ass.CreateInstance(namesp + "." + classname);                obj = ass.CreateInstance(type.FullName);                foreach (MethodInfo item in listMethodInfo)                {                    if (item.Name == methodName && item.GetParameters().Length == 0)//无参数 非静态方法                    {                        item.Invoke(obj, null);//执行方法                    }                }                #region 静态无参数方法  尚未做处理                //静态无参数方法  尚未做处理                //MethodInfo method = type.GetMethod(methodName);                //method.Invoke(null, null);                #endregion            }            catch (Exception e)            {                errorMessage = e.Message;            }            return errorMessage;        }        #region 有参数、无参数、静态有参数、静态无参数        /// <summary>        /// 有参数、无参数、静态有参数、静态无参数        /// </summary>        /// <param name="path"></param>        /// <param name="namesp"></param>        /// <param name="classname"></param>        /// <param name="methodName"></param>        /// <param name="paraType"></param>        /// <param name="paras"></param>        /// <returns></returns>        public string DoReflection(string path,string namesp,string classname,string methodName,string paraType,string paras)         {            string errorMessage = "OK";            try            {                Assembly ass;                Type type;                Object obj;                //Used to test the static method                  Object any = new Object();                //Load the dll                  //Must indicates the whole path of dll                //ass = Assembly.LoadFile(@"E:\Service\WindowsService1\Reflection\bin\Debug\Reflection.dll");                ass = Assembly.LoadFile(@"" + path + "");                //Must be Namespace with class name                  //type = ass.GetType("Reflection.Reflectioncs");                type = ass.GetType(namesp + "." + classname);                MethodInfo method;                if (paraType == "")//方法无参数                {                    method = type.GetMethod(methodName);                    method.Invoke(null, null);                  }                else//方法有参数                {                    string[] pa = paraType.Split(',');//参数类型                    string[] para = paras.Split(',');                    Type[] newtype = new Type[pa.Length];                    Object[] parametors = new Object[para.Length];                    for (int i = 0; i < pa.Length; i++)                    {                        if (pa[i].ToLower() == "string")//字符型                        {                            newtype[i] = typeof(string);                            parametors[i] = para[i].ToString();                        }                        if (pa[i].ToLower() == "int")//整型                        {                            newtype[i] = typeof(int);                            parametors[i] = int.Parse(para[i]);                        }                        if (pa[i].ToLower() == "double")//                        {                            newtype[i] = typeof(double);                            parametors[i] = double.Parse(para[i]);                        }                        if (pa[i].ToLower() == "datetime")//日期类型                        {                            newtype[i] = typeof(DateTime);                            parametors[i] = DateTime.Parse(para[i]);                        }                        if (pa[i].ToLower() == "float")//                        {                            newtype[i] = typeof(float);                            parametors[i] = float.Parse(para[i]);                        }                          }                    method = type.GetMethod(methodName, newtype);//根据参数类型来解决方法重载问题//MethodInfo method = type.GetMethod("WriteString", new Type[] { typeof(string) });                    //Since the WriteTest Class is not Static you should Create the instance of this class                     obj = ass.CreateInstance(namesp + "." + classname);                    Object ret = method.Invoke(                    obj,//Instance object of the class need to be reflect                      parametors);//Parametors of indicated method                  }                }            catch (Exception e)            {                errorMessage = e.Message;            }            return errorMessage;        }        #endregion    }}