您的位置:首页 > 编程语言 > C#

C# 反射实现步骤

2013-11-14 10:32 162 查看
实现步骤:
1,导入using System.Reflection;
2,Assembly.Load("程序集")加载程序集,返回类型是一个Assembly
3,   foreach (Type type in assembly.GetTypes())
{
string t = type.Name;
}
得到程序集中所有类的名称
4,Type type = assembly.GetType("程序集.类名");获取当前类的类型
5,Activator.CreateInstance(type); 创建此类型实例
6,MethodInfo mInfo = type.GetMethod("方法名");获取当前方法
7,mInfo.Invoke(null,方法参数);
        private static void SwapFileDetailDetialE(List<URLValueModel> listFromURLValue, List<URLValueModel> listToURLValue){LogHelper lh = new LogHelper();try{string dllName = listFromURLValue[0].strValue;string keyName = listFromURLValue[1].strValue;Assembly assebly = Assembly.LoadFrom(System.Windows.Forms.Application.StartupPath + "\\" + dllName);Type type = assebly.GetType(keyName);assebly.CreateInstance(type.ToString());foreach (var method in listToURLValue){MethodInfo mInfo = type.GetMethod(method.ToString());mInfo.Invoke(null, null);}lh.WriteLog("The Dll "+dllName +"excute success!");}catch (Exception ex){lh.WriteLog("DLL invoke exception.The message is "+ex.Message);}}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: