1. 程式人生 > >typeof(C# 參考)C# 反射 通過類名建立類例項

typeof(C# 參考)C# 反射 通過類名建立類例項

C# 反射 通過類名建立類例項2011-12-24 上午 12:34“反射”其實就是利用程式集的元資料資訊。

反射可以有很多方法,編寫程式時請先匯入

System.Reflection 名稱空間,假設你要反射一個 DLL 中的類,並且沒有引用它(即未知的型別):

Assembly assembly = Assembly.LoadFile("程式集路徑,不能是相對路徑"); // 載入程式集(EXE 或 DLL)

 object obj = assembly.CreateInstance("類的完全限定名(即包括名稱空間)"); // 建立類的例項

若要反射當前專案中的類可以為:

 Assembly assembly = Assembly.GetExecutingAssembly(); // 獲取當前程式集

object obj = assembly.CreateInstance("類的完全限定名(即包括名稱空間)");

// 建立類的例項,返回為 object 型別,需要強制型別轉換 也可以為:

Type type = Type.GetType("類的完全限定名"); object obj = type.Assembly.CreateInstance(type); ======================================================= 補充:1)反射建立某個類的例項時,必須保證使用類的完全限定名(名稱空間 + 類名)。Type.GetType 方法返回 null 則意味搜尋元資料中的相關資訊失敗(反射失敗),請確保反射時使用類的完全限定名。2)反射功能十分強大,沒有什麼不能實現的。若實現“跨程式集”,請使用第一種方法建立類的例項,並反射該例項的欄位、屬性、方法、事件... 然後動態呼叫之。

主頁
技術資源庫
學習
下載
支援
社群  登入 | 中國(簡體中文) |  | 
  

MSDN Library
開發工具和語言
Visual Studio 2010
Visual Studio
Visual Studio 語言
Visual Basic 和 Visual C#
Visual C#
C# 參考
C# 關鍵字
運算子關鍵字(C# 參考)
as(C# 參考)
is(C# 參考)
new(C# 參考)
sizeof(C# 參考)
typeof(C# 參考)
true(C# 參考)
false(C# 參考)
stackalloc(C# 參考)
 社群內容
 新增程式碼示例和提示以增強此主題。
更多...
 此文章由人工翻譯。 將游標移到文章的句子上,以檢視原文。  譯文
原文

typeof(C# 參考)
 Visual Studio 2010
 其他版本
 此主題尚未評級 評價此主題


用於獲取型別的 System.Type 物件。 typeof 表示式採用以下形式:
System.Type type = typeof(int);
備註

若要獲取表示式的執行時型別,可以使用 .NET Framework 方法 GetType,如以下示例中所示:
int i = 0;
System.Type type = i.GetType();

不能過載 typeof 運算子。

typeof 運算子也能用於公開的泛型型別。 具有不止一個型別引數的型別的規範中必須有適當數量的逗號。 下面的示例演示如何確定方法的返回型別是否是泛型 IEnumerable(Of T)。 假定此方法是 MethodInfo 型別的例項:
string s = method.ReturnType.GetInterface
    (typeof(System.Collections.Generic.IEnumerable<>).FullName);
示例
C#

public class ExampleClass
{
   public int sampleMember;
   public void SampleMethod() {}

   static void Main()
   {
      Type t = typeof(ExampleClass);
      // Alternatively, you could use
      // ExampleClass obj = new ExampleClass();
      // Type t = obj.GetType();

      Console.WriteLine("Methods:");
      System.Reflection.MethodInfo[] methodInfo = t.GetMethods();

      foreach (System.Reflection.MethodInfo mInfo in methodInfo)
         Console.WriteLine(mInfo.ToString());

      Console.WriteLine("Members:");
      System.Reflection.MemberInfo[] memberInfo = t.GetMembers();

      foreach (System.Reflection.MemberInfo mInfo in memberInfo)
         Console.WriteLine(mInfo.ToString());
   }
}
/*
 Output:
    Methods:
    Void SampleMethod()
    System.String ToString()
    Boolean Equals(System.Object)
    Int32 GetHashCode()
    System.Type GetType()
    Members:
    Void SampleMethod()
    System.String ToString()
    Boolean Equals(System.Object)
    Int32 GetHashCode()
    System.Type GetType()
    Void .ctor()
    Int32 sampleMember
*/

此示例使用 GetType 方法確定用來包含數值計算的結果的型別。 這取決於結果數字的儲存要求。
C#

class GetTypeTest
{
    static void Main()
    {
        int radius = 3;
        Console.WriteLine("Area = {0}", radius * radius * Math.PI);
        Console.WriteLine("The type is {0}",
                          (radius * radius * Math.PI).GetType()
        );
    }
}
/*
Output:
Area = 28.2743338823081
The type is System.Double
*/


C# 語言規範


有關更多資訊,請參見 C# 語言規範。C# 語言規範是 C# 語法和用法的權威資料。
請參見
參考
C# 關鍵字
is(C# 參考)
運算子關鍵字(C# 參考)
System.Type
概念
C# 程式設計指南
其他資源
C# 參考
 本文是否對您有所幫助? 是 否
社群內容 新增
 常見問題
 © 2012 Microsoft. 版權所有。
保留所有權利 | 商標 | 隱私權宣告 | 個人資訊中心 | 法律資訊 | MSDN Flash 中心 | 聯絡我們 |  網站反饋  
Assume that method is an instance of a MethodInfo type: