不知道大家有過這樣類似的編碼
? 1
Type type=typeof(T);//T是傳入的類型
這樣寫已經是在潛意思的使用反射了。不管你是否知道,但是這是事實。
Type是一個抽象類,必須進行實例化,而typeof則是返回這個實例化的對象,正好符合了Type要求,而且Type也提供了訪問對象的能力,包括屬性,方法,字段等。對應的為FieldInfo,PropertyInfo和MethodInfo,以及MemberInfo。它們的關系為MemberInfo為基類,其他類為繼承它。
以上為引子,我們來看一個例子,獲取對象的描述信息
這裡定義一個類【五年的指標】,並添加屬性描述Description,使用的是擴展特性類Description。這裡不細說。
? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
public class FiveYear
{
/// <summary>
/// 指標名稱
/// </summary>
[Description("指標名稱")]
public string IndicatorName { get; set; }
/// <summary>
/// 指標顯示名稱
/// </summary>
[Description("指標顯示名稱")]
public string IndicatorDisplayName { get; set; }
/// <summary>
/// 第一年指標值
/// </summary>
[Description("第一年指標值")]
public decimal FirstYearValue { get; set; }
/// <summary>
/// 第二年指標值
/// </summary>
[Description("第二年指標值")]
public decimal SecondYearValue { get; set; }
/// <summary>
/// 第三年指標值
/// </summary>
[Description("第三年指標值")]
public decimal ThirdYearValue { get; set; }
/// <summary>
/// 第四年指標值
/// </summary>
[Description("第四年指標值")]
public decimal ForthYearValue { get; set; }
/// <summary>
/// 第五年指標值
/// </summary>
[Description("第五年指標值")]
public decimal FifthYearValue { get; set; }
}
我們通過反射獲取它的各個屬性名以及描述
首先通過Assembly反射出當前程序集
? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
Assembly demoAssebly= Assembly.GetExecutingAssembly();
Type fiveYears = typeof(FiveYear);//獲取當前實例
Object fiveyearObject= demoAssebly.CreateInstance(fiveYears.FullName);//創建實例對象
PropertyInfo[] properties = fiveyearObject.GetType().GetProperties(BindingFlags.Instance | BindingFlags.Public);//獲得實例對象公共屬性
string tStr=string.Empty;
tStr += string.Format("類名稱 :{0}", fiveYears.Name);
foreach (var item in properties)
{
string name = item.Name; //名稱
object value = item.GetValue(obj2, null); //值
string des = ((DescriptionAttribute)Attribute.GetCustomAttribute(item, typeof(DescriptionAttribute))).Description;// 屬性值
if (item.PropertyType.IsValueType || item.PropertyType.Name.StartsWith("String"))
{
tStr += string.Format("\n屬性名:{0} 對應值 :{1} 屬性顯示名稱:{2},", name, value, des); }
}
獲得的對應信息顯示為下圖

反射的用途除了獲取對象對應的信息,還可以動態創建對象,調用對象的方法<喎�"http://www.2cto.com/kf/ware/vc/" target="_blank" class="keylink">vcD4KPHA+u7nKx7jVssW1xMDgo6zM7bzT0ru49r6yzKy3vbeoPC9wPgo8cD48L3A+CjxwcmUgY2xhc3M9"brush:java;">public static void Add(int x, int y) { int total = x + y; Console.WriteLine(string.Format("【Add】 {0} plus {1} equals to {2}",x,y,total )); }
調用類的方法由兩種方式:分別為
Type 1 InvokeMember()
2 GetMethond()
然後根據方法簽名找打自己調用的方法
實例代碼
? 1 2 3
Type fiveYears =typeof(FiveYear);
object[] paramters = { 12, 3 };//創建參數
fiveYears.InvokeMember(fiveYears.GetMethod("Add").Name,BindingFlags.InvokeMethod, null, fiveYears, paramters);

public objectInvokeMember(string name, BindingFlags invokeAttr, Binder binder, objecttarget, object[] args);
說明 :
fiveYears.GetMethod("Add").Name獲取方法的簽名
第三個參數Binder,封裝了綁定對象的規則,幾乎一直為null,使用內置的DefaultBinder。
反射的使用有時候可以很大程度上減少代碼量,復用率也是有所提高的,並且靈活度也是很好的,但是有時候避免不了效率的問題。這個還是需要看情況的。以上的兩點為常用的方法,尤其為通過名稱調用方法或屬性。這裡拋磚引玉,歡迎大家指點。