Type.IsSubclassof(Type type)
作用:用來確定 一個類是否派生自另一個類/ValueType/Enum/委托
不能用于確定:接口是派生自另一個接口,還是類實現接口
class A{}
class B : A{}A a;
B b;var boo = b.GetType().IsSubclassOf(typeof(A)) // ture
Type.IsAssignableFrom(Type type)
作用:用于確定 接口是派生自另一個接口,還是類實現接口
class A{}
class B : A{}A a;
B b;var boo = a.GetType().IsAssignable(typeof(b)) // ture
自定義關系判斷
/// <summary>/// 兼顧類與類 類與接口 接口與接口/// </summary>/// <param name="targetType">目標子類型</param>/// <param name="targetBaseType">目標基類型</param>/// <returns></returns>public static bool IsAssignableType(Type targetType, Type targetBaseType){return targetType?.IsSubclassOf(targetBaseType) != false || targetBaseType?.IsAssignableFrom(targetType) != false;}
interface IData { }
class A : IData { }
class B : A { } var boo = FEditorUtility.IsAssignableType(typeof(B), typeof(A));
Debug.Log(boo);//true
boo = typeof(B).IsSubclassOf(typeof(A));
Debug.Log(boo);//true
boo = typeof(B).IsSubclassOf(typeof(IData));
Debug.Log(boo);//false
boo = typeof(A).IsAssignableFrom(typeof(B));
Debug.Log(boo);//true
boo = typeof(IData).IsAssignableFrom(typeof(B));
Debug.Log(boo);//true