名詞解釋:
Type類,是一個用來存儲類型的特性和資訊的類。對于程式中的每一個類型,都會有他自己的類資訊,而根據Type提供的屬性和方法獲得這個類型的一切資訊,包括字段,屬性,事件,參數,構造函數等。
生成Type 對象
(1)Type t = typeof(String),通過擷取具體類的type。注意typeof 是運算符合,typeof(x)中的x 是具體的必須是具體的類名、類型名稱等(int ,string,自定義類等等),不可以是變量名稱。
(2)Type t = value.GetType(),對象調用基類基類System.Object的方法的方法 GetType。
Type常用資訊:
這個主要可以具體看Type類文檔, 這個類型 的所有資訊基本都可以擷取的。這裡列些常用的:
/類的名稱
string name = type.Name;
//類的命名空間
string space = type.Namespace;
//類的程式集
Assembly assembly = type.Assembly;
//類的共有字段
FieldInfo[] fieldInfos = type.GetFields();
//擷取具體的共有字段
FieldInfo fieldInfos = type.GetField(string name);
//類的屬性
PropertyInfo[] propertyInfos = type.GetProperties();
//類的方法
MethodInfo[] methodInfos = type.GetMethods();
//具體的方法
MethodInfo methodInfo = type.GetMethod(string name);
//所有公共成員
MemberInfo[] memberInfo = type.GetMembers()
//擷取具體共總成員
MemberInfo[] memberInfo = type.GetMember(string name);
FieldInfo:
FieldInfo.GetValue(Object) Method 擷取的屬性的值,參數Object:将傳回其字段值的對象。