Type type = Type.GetType(dataByJson.MarkClass); // 类名:带命名空间的全名,不然会找不到类 例如 Murray.ClassName
object obj = System.Activator.CreateInstance(type); // 通过类型创建实例
// 通过方法名,得到方法信息GuideMarkPosByID
//MethodInfo method = type.GetMethod("GuideMarkPosByID", new Type[] {}); // 方法无参
MethodInfo method = type.GetMethod("GuideMarkPosByID", new Type[] {typeof(int)}); // 方法有一个类型为int的参数 (有几个参数,就按照对应类型声明几个)
// 调用方法需要传递的参数列表
//object[] parameters = null; // 调用无参方法时候传一个空object数组
object[] parameters = {int.Parse(guidedata.Parameter)}; // 调用有一个参数类型为int的方法传的参数数组 (方法对应有几个参数就声明几个)
// 调用方法
//method.Invoke(obj, parameters); // 方法无返回值
var result = (string)method.Invoke(obj, parameters); // 方法有返回值,返回值类型为string
PlayerManager.Instance.SetTargetPoint(MacroMethod.Instance.ConvertString2Vector(result));
Unity C# 通过反射,通过类名、方法名调用方法
最新推荐文章于 2024-04-17 15:49:00 发布