反射reflect

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
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值