隨便建一個類
寫上方法
void Start () { //var types = Assembly.GetEntryAssembly().GetTypes(); var types = Assembly.GetCallingAssembly().GetTypes(); var aType = typeof(A); Debug.Log(aType.FullName); List<A> alist = new List<A>(); foreach (var type in types) { var baseType = type.BaseType; //獲取基類 while (baseType != null) //獲取所有基類 { Debug.Log(baseType.Name); if (baseType.Name == aType.Name) { Type objtype = Type.GetType(type.FullName, true); object obj = Activator.CreateInstance(objtype); if (obj != null) { A info = obj as A; alist.Add(info); } break; } else { baseType = baseType.BaseType; } } } foreach (var item in alist) { item.a();//執行方法 } }
然后建幾個類測試一下
public abstract class A { public abstract void a(); } public class AA:A { public override void a() { Debug.Log("AA......."); } } public class BB : A { public override void a() { Debug.Log("BB......."); } } public class CC : A { public override void a() { Debug.Log("CC......."); } }
這樣就可以調用所有子類中的a方法了