isAssignableFrom(typeof())
isAssignableFrom(typeof())
1、反射相关
Type——用于获取,类的所有信息,字段,属性,方法等等
Assembly——用于获取程序集,通过程序集获取Type
Activator——用于快速实例化对象
2、判断一个类型的对象是否可以让另一个类型为自己分配空间
父类装子类,是否可以从某一个类型的对象,为自己分配空间
Type fatherType = typeof(Father)
Type sonType = typeof(Son)
// 调用者通过该方法判断是否可以通过传入的类型为自己分配空间
if (fatherType.IsAssignableFrom(sonType)){
print("Is ok.");
Father f= Activator.CreateInstance(sonType) as Father;
print(f); // 输出子类的类名
}
3、通过反射获取泛型类型
List<string> list = new List<string>();
// 得到对象的type信息,包括类名与泛型类型
Type listType = list.GetType();
// 得到具体的泛型类型
Type[] types = listType.getGenericArguments();
print(types[0]); // string
Dictionary<string, float> dic= new Dictionary<string, float>();
Type dicType = dic.GetType();
Type[] types = dicType.getGenericArguments();
print(types[0]); // string
print(types[1]); // single
反射3剑客
Type 用于获取类的所有信息,字段,属性,方法
Assembly 用于获取程序集,通过程序集获取Type
Activator 用于快速实例化对象
判断一个类型的对象是否可以让另一个类型为自己分配控件
父类装子类
IsAssignableFrom()
通过反射获取泛型类型