Assembly.GetType 方法
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
取得 Type 代表指定型態的物件。
多載
| 名稱 | Description |
|---|---|
| GetType(String, Boolean, Boolean) |
取得 Type 組合語言實例中指定名稱的物件,並提供忽略該案例及若找不到該類型時拋出例外的選項。 |
| GetType(String, Boolean) |
在組合語言實例中取得 Type 指定名稱的物件,若找不到該型別,則可選擇性拋出例外。 |
| GetType() | |
| GetType(String) |
取得 Type 組合語言實例中指定名稱的物件。 |
GetType(String, Boolean, Boolean)
取得 Type 組合語言實例中指定名稱的物件,並提供忽略該案例及若找不到該類型時拋出例外的選項。
public:
virtual Type ^ GetType(System::String ^ name, bool throwOnError, bool ignoreCase);
public virtual Type GetType(string name, bool throwOnError, bool ignoreCase);
public Type GetType(string name, bool throwOnError, bool ignoreCase);
override this.GetType : string * bool * bool -> Type
Public Overridable Function GetType (name As String, throwOnError As Boolean, ignoreCase As Boolean) As Type
Public Function GetType (name As String, throwOnError As Boolean, ignoreCase As Boolean) As Type
參數
- name
- String
該類型的完整名稱。
- throwOnError
- Boolean
true 若找不到該型別,則拋出例外; false 返回 null。
- ignoreCase
- Boolean
true忽略型別名稱的情況;否則,。 false
傳回
一個代表指定類別的物件。
實作
例外狀況
name 是 null。
throwOnError 是 true,且無法找到型態。
name 需要一個無法找到的依賴組件。
name 需要相依的組裝檔,但該檔案並非當前載入執行時的有效組合語言。
備註
此方法僅搜尋目前的組裝實例。 參數 name 包含命名空間,但不含組裝。 若要在其他組件中搜尋型別,請使用 Type.GetType(String) 方法過載,該方法可選擇性地將組件顯示名稱作為型別名稱的一部分。
在 .NET Core/.NET 5+ 中,如果類型名稱字串中有組合語言限定的通用型別參數,這些組合體參考會被呼叫 Assembly.GetType 的方法的 AssemblyLoadContext 載入,或如果設定了 AssemblyLoadContext.CurrentContextualReflectionContext 上下文,則載入。
Note
如果該型別已被轉發到另一個組裝,仍會以此方法回傳。 關於型別轉發的資訊,請參見 《通用語言執行時中的型別轉發》。
參數 throwOnError 只影響當未找到該型別時會發生什麼。 這不會影響其他可能被拋出的例外情況。 特別地,若找到型別但無法載入,TypeLoadException即使 是 false,仍可拋出 throwOnError 。
適用於
GetType(String, Boolean)
在組合語言實例中取得 Type 指定名稱的物件,若找不到該型別,則可選擇性拋出例外。
public:
virtual Type ^ GetType(System::String ^ name, bool throwOnError);
public virtual Type GetType(string name, bool throwOnError);
override this.GetType : string * bool -> Type
Public Overridable Function GetType (name As String, throwOnError As Boolean) As Type
參數
- name
- String
該類型的完整名稱。
- throwOnError
- Boolean
true 若找不到該型別,則拋出例外; false 返回 null。
傳回
一個代表指定類別的物件。
實作
例外狀況
name 是 null。
throwOnError 是 true,且無法找到型態。
name 需要一個無法找到的依賴組件。
name 需要相依的組裝檔,但該檔案並非當前載入執行時的有效組合語言。
備註
此方法僅搜尋目前的組裝實例。 參數 name 包含命名空間,但不含組裝。 若要在其他組件中搜尋型別,請使用 Type.GetType(String) 方法過載,該方法可選擇性地將組件顯示名稱作為型別名稱的一部分。
在 .NET Core/.NET 5+ 中,如果類型名稱字串中有組合語言限定的通用型別參數,這些組合體參考會被呼叫 Assembly.GetType 的方法的 AssemblyLoadContext 載入,或如果設定了 AssemblyLoadContext.CurrentContextualReflectionContext 上下文,則載入。
Note
如果該型別已被轉發到另一個組裝,仍會以此方法回傳。 關於型別轉發的資訊,請參見 《通用語言執行時中的型別轉發》。
參數 throwOnError 只影響當未找到該型別時會發生什麼。 這不會影響其他可能被拋出的例外情況。 特別地,若找到型別但無法載入,TypeLoadException即使 是 false,仍可拋出 throwOnError 。
適用於
GetType(String)
取得 Type 組合語言實例中指定名稱的物件。
public:
virtual Type ^ GetType(System::String ^ name);
public virtual Type GetType(string name);
override this.GetType : string -> Type
Public Overridable Function GetType (name As String) As Type
參數
- name
- String
該類型的完整名稱。
傳回
代表指定類別的物件,或 null 是找不到該類別時的物件。
實作
例外狀況
name 無效。
name 是 null。
name 需要一個無法找到的依賴組件。
name 需要相依的組裝檔,但該檔案並非當前載入執行時的有效組合語言。
範例
以下範例定義了命名空間中的Transportation一個抽象MeansOfTransportation類別。 它會呼叫 GetType(String) 取回物件 Type 的方法,呼叫 Type.GetProperties 該方法取得代表該型別屬性的物件陣列 PropertyInfo ,然後顯示該型別抽象屬性的資訊。 請注意,呼叫該 GetType(String) 方法時使用了該型別的完全限定名稱(即其命名空間與型別名稱)。
using System;
using System.Reflection;
public class Example
{
public static void Main()
{
Assembly assem = typeof(Example).Assembly;
Type t = assem.GetType("Transportation.MeansOfTransportation");
if (t != null)
{
Console.WriteLine($"Virtual properties in type {t.FullName}:");
PropertyInfo[] props = t.GetProperties();
int nVirtual = 0;
for (int ctr = 0; ctr < props.Length; ctr++)
{
if (props[ctr].GetMethod.IsVirtual)
{
Console.WriteLine($" {props[ctr].Name} (type {props[ctr].PropertyType.FullName})");
nVirtual++;
}
}
if (nVirtual == 0)
Console.WriteLine(" No virtual properties");
}
}
}
namespace Transportation
{
public abstract class MeansOfTransportation
{
abstract public bool HasWheels { get; set; }
abstract public int Wheels { get; set; }
abstract public bool ConsumesFuel { get; set; }
abstract public bool Living { get; set; }
}
}
// The example displays the following output:
// Virtual properties in type Transportation.MeansOfTransportation:
// HasWheels (type System.Boolean)
// Wheels (type System.Int32)
// ConsumesFuel (type System.Boolean)
// Living (type System.Boolean)
Imports System.Reflection
Module Example
Public Sub Main()
Dim assem As Assembly = GetType(Example).Assembly
Dim t As Type = assem.GetType("Transportation.MeansOfTransportation")
If Not t Is Nothing Then
Console.WriteLine("Virtual properties in type {0}:",
t.FullName)
Dim props() As PropertyInfo = t.GetProperties()
Dim nVirtual As Integer = 0
For ctr As Integer = 0 To props.Length - 1
If props(ctr).GetMethod.IsVirtual Then
Console.WriteLine(" {0} (type {1})",
props(ctr).Name,
props(ctr).PropertyType.FullName)
nVirtual += 1
End If
Next
If nVirtual = 0 Then
Console.WriteLine(" No virtual properties")
End If
End If
End Sub
End Module
Namespace Transportation
Public MustInherit Class MeansOfTransportation
Public MustOverride Property HasWheels As Boolean
Public MustOverride Property Wheels As Integer
Public MustOverride Property ConsumesFuel As Boolean
Public MustOverride Property Living As Boolean
End Class
End Namespace
' The example displays the following output:
' Virtual properties in type Transportation.MeansOfTransportation:
' HasWheels (type System.Boolean)
' Wheels (type System.Int32)
' ConsumesFuel (type System.Boolean)
' Living (type System.Boolean)
備註
此方法僅搜尋目前的組裝實例。 參數 name 包含命名空間,但不含組裝。 若要在其他組件中搜尋型別,請使用 Type.GetType(String) 方法過載,該方法可選擇性地將組件顯示名稱作為型別名稱的一部分。
在 .NET Core/.NET 5+ 中,如果類型名稱字串中有組合語言限定的通用型別參數,這些組合體參考會被呼叫 Assembly.GetType 的方法的 AssemblyLoadContext 載入,或如果設定了 AssemblyLoadContext.CurrentContextualReflectionContext 上下文,則載入。
Note
如果該型別已被轉發到另一個組裝,仍會以此方法回傳。 關於型別轉發的資訊,請參見 《通用語言執行時中的型別轉發》。