码迷,mamicode.com
首页 > Windows程序 > 详细

c#反射

时间:2019-07-10 20:06:18      阅读:142      评论:0      收藏:0      [点我收藏+]

标签:字符串   creat   null   tar   lin   type   ict   inf   数组   


namespace
_20190710 { class Class1 { static Object obj; static Dictionary<string, MethodInfo> dic = new Dictionary<string, MethodInfo>(); public static void addEventListener(string className, string priex = "do") { //string classname = "_20190710.A"; //根据字符串类名,获取该类的类型 Type t = Type.GetType(className); //创建对象 obj = Activator.CreateInstance(t); //获得所有方法,返回一个方法数组 //或者返回单个方法MethodInfo m = t.GetMethod(str); MethodInfo[] m = t.GetMethods(); //取出所有指定开头的方法 for (int i = 0; i < m.Length; i++) { MethodInfo mth = m[i]; if (mth.Name.StartsWith(priex)) { dic.Add(mth.Name, mth); Console.WriteLine(mth.Name); } } } public static void dispatchEvent(string methodName) { if (dic.ContainsKey(methodName)) { MethodInfo method = dic[methodName]; method.Invoke(obj, null); } } }

 

c#反射

标签:字符串   creat   null   tar   lin   type   ict   inf   数组   

原文地址:https://www.cnblogs.com/XianCao/p/11166113.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!