C# - 反射

來源:互聯網
上載者:User

標籤:static   code   ctypes   安全性   儲存   get   cti   font   style   

反射(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# - 反射

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.