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

【C#】通过反射调用某类某方法

2016-10-10 14:26 337 查看
string strClass = “VolumeManager”; //类名,有命名空间需要加上,namespace.class

string callMethodName = toggle.transform.parent.name; //方法名

Type t;//类型

object obj; //对象

t = Type.GetType(strClass); //通过string找到类

System.Reflection.MethodInfo method = t.GetMethod(callMethodName);//通过类找到方法method

obj = System.Activator.CreateInstance(t); //new一个类的对象

method.Invoke(obj, null); //用这个对象调用method方法
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  C#-反射