1. 程式人生 > >C# - 反射

C# - 反射

security cache 版本 get str sse getmethod typeof運算符 var

反射(Reflection)

反射是一種機制,由.net framework類庫提供的一種能顯示出某個程序集中的元數據的機制。要利用反射,首先需要獲得Type實例,Type類表示任意類型的相關信息,它提供一系列屬性和方法用於獲取某個類型的元數據,可以獲取某個類的成員信息並設置或修改屬性成員、調用成員方法,如果設置了恰當的代碼訪問安全性(Code Access Security)權限,反射還可以繞過可訪問性規則,比如訪問private、protected的成員

獲取Type對象

有兩種方式可以獲取類型的Type表示,任意類型的對象調用GetType()方法或使用typeof運算符 Animal
a = new Animal ( );

Type typeForAnimal1=a.GetType ( );
Type typeForAnimal2 = typeof ( Animal );
bool Test = typeForAnimal1 == typeForAnimal2;

Assembly類(System.Reflection.Assembly)

表示對程序集的描述,使用它可以很方便的將某個程序集載入到當前應用程序域中從而獲取該程序集的信息

//屬性

CodeBase
//返回一個字符串表示原先所指定的程序集的實際位置

EntryPoint
//返回一個MethodInfo實例表示當前加載的程序集的入口

Evidence
//返回一個Evidence實例,該實例包含了簽名和代碼初始位置的信息

FullName
//返回一個字符串表示當前加載的程序集的名稱、版本號、文化標識

GlobalAssemblyCache
//返回一個bool值表示當前加載的程序集是否是從全局程序集緩存中加載進來的(全局程序集緩存是用來存儲被多個應用程序所共享的程序集)

Location
//返回一個字符串表示當前加載的程序集的路徑 //靜態方法

CreateQualifiedName ( )
//創建該類型所在的程序集的顯示名稱所限定的類型的名稱
//假設Tree定義在Forest程序集中,則該方法返回Tree.Forest

GetAssembly
//返回一個Acssemly實例,該實例是根據參數提供的類型生成的對象

Type類(System.Reflection.Type

獲取任意類型的Type表示。

//以下的GetMethod、GetMethods、GetProperty、GetProperties方法具有BindingFlags的參數,用以指定需要搜索的成員
//BindingFlags枚舉可能的值如下
//1.Public : 只獲取公共的成員
//2.NoPublic : 只獲取非公共的成員
//3.Instance : 只獲取對象的成員
//4.Static : 獲取類的成員
//5.DeclaredOnly : 不獲取繼承的成員
//BindingFlags枚舉必須用項1或項2組合其它項,否則只會獲取到null
GetMethods ( )
//返回一個MethodInfo數組,存儲被搜索到的方法的信息
//示例:
//只獲取類的公共的實例方法,不包括繼承
Type t = typeof ( Animal );
MethodInfo [ ] methods = t.GetMethods ( BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly );

GetProperties ( )
//返回一個PropertyInfo數組,存儲被搜索到的屬性的信息

GetMethod ( string methodName )
//獲取該類型的單個方法的信息,返回一個MethodInfo對象

GetProperty ( string propertyName )
//獲取該類型的單個屬性的信息,返回一個PropertyInfo對象

IsGenericType
//判斷當前Type對象是否是泛型或泛型方法
//示例:
Type type = typeof ( Animal<string> );
Console.Write ( type.IsGenericType );//print true

ContainsGenericParameters
//判斷某個類型或某個方法的類型參數是否還未具有實參
//示例:
Type type = typeof ( Animal<> );
Console.Write ( type.ContainsGenericParameters );//print true
Type type = typeof ( Animal<string> );
Console.Write ( type.ContainsGenericParameters );//print false

GetGenericArguments()
//獲取泛型、泛型方法的類型參數
//示例:
Animal<string> a = new Animal<string> { };
Type type = typeof ( Animal<string> );
Type [ ] AnimalGenericTypes = type.GetGenericArguments ();
foreach(Type AnimalGenericType in AnimalGenericTypes )
{
Console.WriteLine ( AnimalGenericType.GetProperties() );
}

MethodInfo類(System.Reflection.MethodInfo

表示方法的信息

Name
//方法名稱

ReturnType.Name
//獲取方法的返回類型的名稱

Invoke ( )
//執行該方法

GetParameters ( )
//獲取方法的形參信息,返回一個ParameterInfo數組存儲所有形參信息
//示例:
//讀取某個類的所有方法
Type x = typeof ( MyClass );
MethodInfo [ ] methods = x.GetMethods ( );
StringBuilder s = new StringBuilder ( );
foreach ( var method in methods )
{
s.AppendFormat ( "<div style=‘margin-top:100px;‘><div>函數名稱:{0}</div>" , method.Name );
s.AppendFormat ( "<div>函數返回類型:{0}</div>" , method.ReturnType.Name );
foreach ( var @params in method.GetParameters ( ) )
{
s.AppendFormat ( "<div style=‘margin-left:100px;‘>函數形參名稱:{0}</div>" , @params.Name );
s.AppendFormat ( "<div style=‘margin-left:100px;‘>函數形參類型:{0}</div>" , @params.ParameterType.Name );
}
s.Append ( "</div>" );
}
Console.WriteLine ( s.ToString ( ) );

ParameterInfo類(System.Reflection.ParameterInfo

表示方法的參數信息。

Name
//形參名稱

ParameterType.Name
//形參類型的名稱

PropertyInfo類(System.Reflection.PropertyInfo

表示屬性的信息。

PropertyType
//獲取屬性的類型
//示例:
PropertyInfo property = typeof ( Animal ).GetProperties ( ) [ 0 ];
bool IsBool=property.PropertyType == typeof ( bool ); //簡單類型判斷這樣寫
bool IsClass = property.PropertyType.IsClass; //復雜類型判斷這樣寫

GetValue ( obj )
//獲取指定對象的屬性值

SetValue ( obj )
//設置指定對象的屬性值
//示例:
Type type = typeof ( Animal );
PropertyInfo property = type.GetProperty ( "Name" );
Animal t = new Animal ( );
string name = property.GetValue ( t ).ToString ( );
Console.WriteLine ( name );

C# - 學習總目錄

C# - 反射