unity通过反射调用方法

using System.Reflection;
using System;

通用

// 1.Load(命名空间名称),GetType(命名空间.类名)
Type type = ClassName.GetType();
//2.GetMethod(需要调用的方法名称)
MethodInfo method = type.GetMethod(“MethodFunc” });
// 3.调用的实例化方法(非静态方法)需要创建类型的一个实例
object obj = Activator.CreateInstance(type);
//4.方法需要传入的参数
object[] parameters = new object[] { };
// 5.调用方法,如果调用的是一个静态方法,就不需要第3步(创建类型的实例)
// 相应地调用静态方法时,Invoke的第一个参数为null
method.Invoke(obj , null);

调自身类

MethodInfo method = this.GetType().GetMethod(“MethodFunc”);
method?.Invoke(this, null);

你可能感兴趣的:(unity通过反射调用方法)