Type 類別
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
代表類型宣告:類別類型、介面類型、陣列類型、值類型、列舉類型、類型參數、泛型類型定義,以及開放式或封閉式的建構泛型類型。
public ref class Type abstract
public ref class Type abstract : System::Reflection::MemberInfo, System::Reflection::IReflect
public ref class Type abstract : System::Reflection::MemberInfo, System::Reflection::IReflect, System::Runtime::InteropServices::_Type
public abstract class Type
public abstract class Type : System.Reflection.MemberInfo, System.Reflection.IReflect
[System.Runtime.InteropServices.ClassInterface(System.Runtime.InteropServices.ClassInterfaceType.None)]
[System.Serializable]
public abstract class Type : System.Reflection.MemberInfo, System.Reflection.IReflect, System.Runtime.InteropServices._Type
[System.Runtime.InteropServices.ClassInterface(System.Runtime.InteropServices.ClassInterfaceType.None)]
[System.Serializable]
[System.Runtime.InteropServices.ComVisible(true)]
public abstract class Type : System.Reflection.MemberInfo, System.Reflection.IReflect, System.Runtime.InteropServices._Type
type Type = class
type Type = class
inherit MemberInfo
interface IReflect
[<System.Runtime.InteropServices.ClassInterface(System.Runtime.InteropServices.ClassInterfaceType.None)>]
[<System.Serializable>]
type Type = class
inherit MemberInfo
interface _Type
interface IReflect
[<System.Runtime.InteropServices.ClassInterface(System.Runtime.InteropServices.ClassInterfaceType.None)>]
[<System.Serializable>]
[<System.Runtime.InteropServices.ComVisible(true)>]
type Type = class
inherit MemberInfo
interface _Type
interface IReflect
Public MustInherit Class Type
Public MustInherit Class Type
Inherits MemberInfo
Implements IReflect
Public MustInherit Class Type
Inherits MemberInfo
Implements _Type, IReflect
- 繼承
-
Type
- 繼承
- 衍生
- 屬性
- 實作
範例
下列範例顯示的一些代表性功能 Type 。 typeof
Visual Basic) 中的 c # 運算子 (GetType
運算子用來取得 Type 代表的物件 String 。 使用這個 Type 物件時, GetMethod 會使用方法來取得, MethodInfo 代表 String.Substring 接受起始位置和長度的多載。
為了識別多載簽章,程式碼範例會建立一個包含兩個物件的暫存陣列, Type 代表 int
Integer
Visual Basic) 中的 (。
程式碼範例會使用 MethodInfo 來叫用 Substring 字串 "Hello,World!" 上的方法,並顯示結果。
#using <System.dll>
using namespace System;
using namespace System::Reflection;
void main()
{
// Get a Type object representing the System.String type.
Type^ t = String::typeid;
MethodInfo^ substr = t->GetMethod("Substring",
gcnew array<Type^> { int::typeid, int::typeid });
Object^ result =
substr->Invoke("Hello, World!", gcnew array<Object^> { 7, 5 });
Console::WriteLine("{0} returned \"{1}\".", substr, result);
}
/* This code example produces the following output:
System.String Substring(Int32, Int32) returned "World".
*/
using System;
using System.Reflection;
class Example
{
static void Main()
{
Type t = typeof(String);
MethodInfo substr = t.GetMethod("Substring",
new Type[] { typeof(int), typeof(int) });
Object result =
substr.Invoke("Hello, World!", new Object[] { 7, 5 });
Console.WriteLine("{0} returned \"{1}\".", substr, result);
}
}
/* This code example produces the following output:
System.String Substring(Int32, Int32) returned "World".
*/
Imports System.Reflection
Module Example
Sub Main()
Dim t As Type = GetType(String)
Dim substr As MethodInfo = t.GetMethod("Substring", _
New Type() { GetType(Integer), GetType(Integer) })
Dim result As Object = _
substr.Invoke("Hello, World!", New Object() { 7, 5 })
Console.WriteLine("{0} returned ""{1}"".", substr, result)
End Sub
End Module
' This code example produces the following output:
'
'System.String Substring(Int32, Int32) returned "World".
備註
Type
是功能的根 System.Reflection ,也是存取中繼資料的主要方式。 您可以使用的成員 Type 來取得型別宣告的相關資訊,例如,型別的成員 (例如) 類別的函式、方法、欄位、屬性和事件,以及部署類別的模組和元件。
不需要任何許可權,程式碼就能使用反映來取得類型和其成員的相關資訊,而不論其存取層級為何。 程式碼不需要任何許可權,就能使用反映來存取公用成員,或是存取層級會在正常編譯期間顯示這些成員的其他成員。 不過,為了讓您的程式碼使用反映來存取通常無法存取的成員,例如私用或內部方法,或您的類別不會繼承之類型的受保護欄位,您的程式碼必須具有 ReflectionPermission 。 請參閱 反映的安全性考慮。
Type
是允許多個實現的抽象基類。 系統一律會提供衍生的類別 RuntimeType
。 在反映中,所有以單字執行時間開頭的類別,只會針對系統中的每個物件建立一次,並支援比較作業。
注意
在多執行緒案例中,請勿鎖定物件,以便 Type 同步處理資料的存取 static
。 其他無法控制的程式碼,也可能會鎖定您的類別類型。 這可能會導致鎖死。 相反地,藉由鎖定私用物件來同步處理靜態資料的存取 static
。
注意
衍生類別可以存取呼叫程式碼基類的受保護成員。 此外,也允許存取呼叫程式碼元件的元件成員。 作為規則,如果您允許存取早期繫結程式碼,則您也可以在晚期繫結程式碼中存取。
注意
擴充其他介面的介面不會繼承擴充介面中定義的方法。
本節內容:
類型物件代表哪些類型?
正在抓取型別物件
比較型別物件是否相等
類型物件代表哪些類型?
此類別為安全線程;多個執行緒可以同時讀取此類型的實例。 類別的實例 Type 可以代表下列任何類型:
類別
值類型
陣列
介面
列舉
委派
結構化泛型型別和泛型型別定義
結構化泛型型別、泛型型別定義和泛型方法定義的類型引數和類型參數
正在抓取型別物件
您 Type 可以透過下列方式取得與特定類型相關聯的物件:
實例 Object.GetType 方法 Type 會傳回代表實例類型的物件。 由於所有的 managed 類型都 Object 是衍生自,因此 GetType 可以在任何類型的實例上呼叫方法。
下列範例會呼叫 Object.GetType 方法,以判斷物件陣列中每個物件的執行時間型別。
using namespace System; void main() { array<Object^>^ values = { "word", true, 120, 136.34 }; for each (Object^ value in values) Console::WriteLine("{0} - type {1}", value, value->GetType()->Name); } // The example displays the following output: // word - type String // True - type Boolean // 120 - type Int32 // 136.34 - type Double
object[] values = { "word", true, 120, 136.34, 'a' }; foreach (var value in values) Console.WriteLine("{0} - type {1}", value, value.GetType().Name); // The example displays the following output: // word - type String // True - type Boolean // 120 - type Int32 // 136.34 - type Double // a - type Char
Module Example Public Sub Main() Dim values() As Object = { "word", True, 120, 136.34, "a"c } For Each value In values Console.WriteLine("{0} - type {1}", value, value.GetType().Name) Next End Sub End Module ' The example displays the following output: ' word - type String ' True - type Boolean ' 120 - type Int32 ' 136.34 - type Double ' a - type Char
靜態 Type.GetType 方法 Type 會傳回物件,該物件表示其完整名稱所指定的型別。
Module.GetTypes、 Module.GetType 和 Module.FindTypes 方法會傳回
Type
物件,這些物件代表模組中定義的類型。 第一個方法可以用來取得 Type 模組中定義之所有公用和私用類型的物件陣列。 (您可以Module
透過 Assembly.GetModule 或 Assembly.GetModules 方法或透過屬性取得的實例 Type.Module 。 )System.Reflection.Assembly物件包含一些方法,用來取出元件中定義的類別,包括 Assembly.GetType 、 Assembly.GetTypes 和 Assembly.GetExportedTypes 。
方法會傳回 FindInterfaces 類型所支援之介面類別型的篩選清單。
GetElementType方法
Type
會傳回代表元素的物件。GetInterfaces和 GetInterface 方法會傳回 Type 代表型別所支援之介面類別型的物件。
方法會傳回 GetTypeArray 物件的陣列, Type 代表由任意一組物件所指定的類型。 物件是以型別的陣列來指定 Object 。
GetTypeFromProgID和 GetTypeFromCLSID 方法是針對 COM 互通性而提供。 它們會傳回 Type 物件,代表由或所指定的型別
ProgID
CLSID
。GetTypeFromHandle提供方法以提供互通性。 它會傳回
Type
代表類別控制碼所指定之類型的物件。c #
typeof
運算子、c + +typeid
運算子和 Visual BasicGetType
運算子會取得類型的Type
物件。MakeGenericType方法 Type 會傳回代表結構化泛型型別的物件,如果其屬性傳回,則為開放式的結構化型別,否則為 ContainsGenericParameters
true
封閉的結構化型別。 泛型型別只有在關閉時才能具現化。MakeArrayType、 MakePointerType 和 MakeByRefType 方法會傳回物件, Type 分別代表指定類型的陣列、指定類型的指標,以及 c # 中 (的參考參數類型(
ref
ByRef
Visual Basic) )。
比較型別物件是否相等
Type代表類型的物件是唯一的,也就是說, Type 只有在代表相同的類型時,兩個物件參考才會參考相同的物件。 這可讓您 Type 使用參考相等來比較物件。 下列範例 Type 會比較代表一些整數值的物件,以判斷它們是否為相同的類型。
using namespace System;
void main()
{
Int64 number1 = 1635429;
Int32 number2 = 16203;
double number3 = 1639.41;
Int64 number4 = 193685412;
// Get the type of number1.
Type^ t = number1.GetType();
// Compare types of all objects with number1.
Console::WriteLine("Type of number1 and number2 are equal: {0}",
Object::ReferenceEquals(t, number2.GetType()));
Console::WriteLine("Type of number1 and number3 are equal: {0}",
Object::ReferenceEquals(t, number3.GetType()));
Console::WriteLine("Type of number1 and number4 are equal: {0}",
Object::ReferenceEquals(t, number4.GetType()));
}
// The example displays the following output:
// Type of number1 and number2 are equal: False
// Type of number1 and number3 are equal: False
// Type of number1 and number4 are equal: True
long number1 = 1635429;
int number2 = 16203;
double number3 = 1639.41;
long number4 = 193685412;
// Get the type of number1.
Type t = number1.GetType();
// Compare types of all objects with number1.
Console.WriteLine("Type of number1 and number2 are equal: {0}",
Object.ReferenceEquals(t, number2.GetType()));
Console.WriteLine("Type of number1 and number3 are equal: {0}",
Object.ReferenceEquals(t, number3.GetType()));
Console.WriteLine("Type of number1 and number4 are equal: {0}",
Object.ReferenceEquals(t, number4.GetType()));
// The example displays the following output:
// Type of number1 and number2 are equal: False
// Type of number1 and number3 are equal: False
// Type of number1 and number4 are equal: True
Module Example
Public Sub Main()
Dim number1 As Long = 1635429
Dim number2 As Integer = 16203
Dim number3 As Double = 1639.41
Dim number4 As Long = 193685412
' Get the type of number1.
Dim t As Type = number1.GetType()
' Compare types of all objects with number1.
Console.WriteLine("Type of number1 and number2 are equal: {0}",
Object.ReferenceEquals(t, number2.GetType()))
Console.WriteLine("Type of number1 and number3 are equal: {0}",
Object.ReferenceEquals(t, number3.GetType()))
Console.WriteLine("Type of number1 and number4 are equal: {0}",
Object.ReferenceEquals(t, number4.GetType()))
End Sub
End Module
' The example displays the following output:
' Type of number1 and number2 are equal: False
' Type of number1 and number3 are equal: False
' Type of number1 and number4 are equal: True
給實施者的注意事項
當您繼承自時 Type
,您必須覆寫下列成員:
GetConstructorImpl(BindingFlags, Binder, CallingConventions, Type[], ParameterModifier[])
GetMethodImpl(String, BindingFlags, Binder, CallingConventions, Type[], ParameterModifier[])
GetPropertyImpl(String, BindingFlags, Binder, Type, Type[], ParameterModifier[])
建構函式
Type() |
初始化 Type 類別的新執行個體。 |
欄位
Delimiter |
分隔 Type 的命名空間中的名稱。 此欄位為唯讀。 |
EmptyTypes |
表示 Type 類型的空陣列。 此欄位為唯讀。 |
FilterAttribute |
表示用在屬性 (Attribute) 上的成員篩選條件。 此欄位為唯讀。 |
FilterName |
表示用於名稱上的有區分大小寫的成員篩選條件。 此欄位為唯讀。 |
FilterNameIgnoreCase |
表示用在名稱上的不區分大小寫的成員篩選條件。 此欄位為唯讀。 |
Missing |
表示在 Type 資訊中的遺漏值。 此欄位為唯讀。 |
屬性
Assembly | |
AssemblyQualifiedName |
取得類型的組件限定名稱,包含載入 Type 的組件名稱。 |
Attributes |
取得與 Type 關聯的屬性。 |
BaseType |
取得目前 Type 所直接繼承的類型。 |
ContainsGenericParameters |
取得值,該值指出目前的 Type 物件是否有尚未被特定類型取代的類型參數。 |
CustomAttributes |
取得包含此成員之自訂屬性的集合。 (繼承來源 MemberInfo) |
DeclaringMethod |
如果目前的 MethodBase 表示泛型方法的型別參數,則取得表示宣告方法的 Type。 |
DeclaringType |
取得宣告目前巢狀類型或泛型型別參數的類型。 |
DefaultBinder |
取得預設繫結器 (Binder) 的參考,它會實作內部規則來選取由 InvokeMember(String, BindingFlags, Binder, Object, Object[], ParameterModifier[], CultureInfo, String[]) 呼叫的適當成員。 |
FullName |
取得類型的完整名稱 (包括其命名空間,但不包括其組件)。 |
GenericParameterAttributes |
取得一組 GenericParameterAttributes 旗標,敘述目前泛型類型參數的共變數與特殊條件約束。 |
GenericParameterPosition |
當 Type 物件表示泛型類型或泛型方法的類型參數時,在宣告參數的泛型類型或泛型方法之類型參數清單中,取得類型參數的位置。 |
GenericTypeArguments |
取得此類型之泛型類型引數的陣列。 |
GUID |
取得與 Type相關聯的 GUID。 |
HasElementType | |
IsAbstract |
取得值,指出 Type 是否為抽象並且必須被覆寫。 |
IsAnsiClass |
取得值,指出是否為 |
IsArray |
取得值,以表示類型是否為陣列。 |
IsAutoClass |
取得值,指出是否為 |
IsAutoLayout |
取得表示目前類型的欄位是否已由 Common Language Runtime 自動配置版面的值。 |
IsByRef |
取得值,指出 Type 是否以傳址方式傳遞。 |
IsByRefLike |
取得值,指出類型是否為 byref-like 結構。 |
IsClass |
取得值,表示 Type 是類別或委派,也就是非實值類型或介面。 |
IsCollectible |
取得指出此 MemberInfo 物件是否為可回收 AssemblyLoadContext 中保存之組件一部分的值。 (繼承來源 MemberInfo) |
IsCOMObject |
取得值,指出 Type 是否為 COM 物件。 |
IsConstructedGenericType |
取得值,指出這個物件是否表示建構的泛型類型。 您可以建立已建構之泛型類型的執行個體。 |
IsContextful |
取得值,指出在內容中是否可以裝載 Type。 |
IsEnum |
取得值,指出目前的 Type 是否表示列舉類型。 |
IsExplicitLayout |
取得表示目前類型的欄位是否已在明確指定之位移配置版面的值。 |
IsGenericMethodParameter |
取得值,指出目前的 Type 是否在泛型方法的定義中代表型別參數。 |
IsGenericParameter |
取得值,指出目前的 Type 是否表示泛型類型或泛型方法定義中的類型參數。 |
IsGenericType |
取得值,指出目前類型是否為泛型類型。 |
IsGenericTypeDefinition |
取得值,指出目前的 Type 是否表示可用於建構其他泛型類型的泛型類型定義。 |
IsGenericTypeParameter |
取得值,指出目前的 Type 是否在泛型型別的定義中代表型別參數。 |
IsImport |
取得值,指出 Type 是否套用了 ComImportAttribute 屬性 (Attribute),亦即其是否從 COM 類型程式庫匯入。 |
IsInterface |
取得值,指出 Type 是否為介面;也就是說,不是類別或實值類型。 |
IsLayoutSequential |
取得表示目前類型的欄位是否已依為其定義或發出至中繼資料之順序,循序配置版面的值。 |
IsMarshalByRef |
取得值,指出 Type 是否以傳址方式封送處理。 |
IsNested |
取得值,表示目前的 Type 物件代表的類型之定義是否位於另一個類型的定義內部。 |
IsNestedAssembly |
取得值,指出 Type 是否為巢狀,並只在它自己的組件內為可見。 |
IsNestedFamANDAssem |
取得值,指出 Type 是否為巢狀,並只對同時屬於它自己家族和它自己組件的類別為可見。 |
IsNestedFamily |
取得值,指出 Type 是否為巢狀,並只在它自己的系列內為可見。 |
IsNestedFamORAssem |
取得值,指出 Type 是否為巢狀並只對屬於它自己家族或它自己組件的類別為可見。 |
IsNestedPrivate |
取得值,指出 Type 是否為巢狀並且宣告為私用。 |
IsNestedPublic |
取得值,指出類別是否為巢狀 (Nest) 並且宣告為公用 (Public)。 |
IsNotPublic |
取得值,指出 Type 是否未宣告為公用。 |
IsPointer |
取得值,指出 Type 是否為指標。 |
IsPrimitive |
取得值,指出 Type 是否為其中一個基本類型 (Primitive Type)。 |
IsPublic |
取得值,指出 Type 是否宣告為公用。 |
IsSealed |
取得值,指出 Type 是否宣告為密封。 |
IsSecurityCritical |
取得值,這個值表示目前類型在目前信任層級上是否為安全性關鍵或安全性安全關鍵,因而可以執行重要的作業。 |
IsSecuritySafeCritical |
取得值,這個值表示目前類型在目前信任層級上是否為安全性安全關鍵,也就是說,它是否能執行重要作業並由安全性透明的程式碼存取。 |
IsSecurityTransparent |
取得值,這個值表示目前類型在目前信任層級上是否為透明,因此無法執行重要作業。 |
IsSerializable |
取得值,指出 Type 是否可序列化。 |
IsSignatureType |
取得值,指出類型是否為特徵標記類型。 |
IsSpecialName |
取得值,表示類型是否具有需要特殊處理的名稱。 |
IsSZArray |
取得值,指出類型是否為陣列類型,且只能代表下限為零的一維陣列。 |
IsTypeDefinition |
取得值,指出類型是否為類型定義。 |
IsUnicodeClass |
取得值,指出是否為 |
IsValueType |
取得值,指出 Type 是否為實值類型。 |
IsVariableBoundArray |
取得值,指出類型是否為陣列類型,且可代表多維陣列或任意下限的陣列。 |
IsVisible |
取得一個值,表示位於組件之外的程式碼是否能存取 Type。 |
MemberType |
取得一個 MemberTypes 值,代表這個成員是類型或巢狀類型。 |
MetadataToken |
取得值,這個值可識別中繼資料項目。 (繼承來源 MemberInfo) |
Module |
在已定義的目前 Type 中取得模組 (DLL)。 |
Name |
在衍生類別中覆寫時,取得目前類型的名稱。 |
Name |
取得目前成員的名稱。 (繼承來源 MemberInfo) |
Namespace |
取得 Type 的命名空間。 |
ReflectedType |
取得用來取得這個成員的類別物件。 |
StructLayoutAttribute |
取得描述目前類型配置的 StructLayoutAttribute。 |
TypeHandle |
取得目前 Type 的控制代碼。 |
TypeInitializer |
取得類型的初始設定式。 |
UnderlyingSystemType |
指示類型,該類型是由表示這個類型的 Common Language Runtime 所提供的。 |
方法
運算子
Equality(Type, Type) |
表示兩個 Type 物件是否相等。 |
Inequality(Type, Type) |
表示兩個 Type 物件是否不相等。 |
明確介面實作
_MemberInfo.GetIDsOfNames(Guid, IntPtr, UInt32, UInt32, IntPtr) |
將一組名稱對應至一組對應的分派識別項 (Dispatch Identifier)。 (繼承來源 MemberInfo) |
_MemberInfo.GetType() |
取得 Type 物件,表示 MemberInfo 類別。 (繼承來源 MemberInfo) |
_MemberInfo.GetTypeInfo(UInt32, UInt32, IntPtr) |
擷取物件的類型資訊,可以用來取得介面的類型資訊。 (繼承來源 MemberInfo) |
_MemberInfo.GetTypeInfoCount(UInt32) |
擷取物件提供的類型資訊介面數目 (0 或 1)。 (繼承來源 MemberInfo) |
_MemberInfo.Invoke(UInt32, Guid, UInt32, Int16, IntPtr, IntPtr, IntPtr, IntPtr) |
提供物件所公開的屬性和方法的存取權。 (繼承來源 MemberInfo) |
_Type.GetIDsOfNames(Guid, IntPtr, UInt32, UInt32, IntPtr) |
將一組名稱對應至一組對應的分派識別項 (Dispatch Identifier)。 |
_Type.GetTypeInfo(UInt32, UInt32, IntPtr) |
擷取物件的類型資訊,可以用來取得介面的類型資訊。 |
_Type.GetTypeInfoCount(UInt32) |
擷取物件提供的類型資訊介面數目 (0 或 1)。 |
_Type.Invoke(UInt32, Guid, UInt32, Int16, IntPtr, IntPtr, IntPtr, IntPtr) |
提供物件所公開的屬性和方法的存取權。 |
擴充方法
適用於
執行緒安全性
此型別具備執行緒安全。