using System;
using System.Collections.Generic;
using System.Reflection;
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#反射
最新推荐文章于 2025-05-09 15:28:08 发布
博客围绕C#反射展开,虽暂无具体内容,但可推测会涉及C#反射的原理、应用等信息技术相关关键信息,反射在C#编程中是重要技术,能实现动态获取类型信息等功能。

1万+

被折叠的 条评论
为什么被折叠?



