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。 Visual Basic) 中的 C# typeof
运算符 (GetType
运算符用于获取Type表示String的对象。 从此Type对象中,该方法GetMethod用于获取表示MethodInfoString.Substring采用起始位置和长度的重载。
为了标识重载签名,代码示例创建一个临时数组,其中包含表示Visual Basic) 中的 (的两Type个对象int
。Integer
代码示例使用在 MethodInfo 字符串“Hello, World!”上调用 Substring 该方法,并显示结果。
#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".
*/
open System
let t = typeof<String>
let substr = t.GetMethod("Substring", [| typeof<int>; typeof<int> |])
let result = substr.Invoke("Hello, World!", [| 7; 5 |])
printfn $"{substr} returned \"{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 对象
比较类型对象是否相等
Type 对象表示哪些类型?
此类是线程安全的;多个线程可以同时从此类型的实例中读取。 类的 Type 实例可以表示以下任一类型:
类
值类型
数组
接口
枚举
委托
构造泛型类型和泛型类型定义
构造泛型类型、泛型类型定义和泛型方法定义的类型参数和类型参数
检索 Type 对象
Type可以通过以下方式获取与特定类型关联的对象:
实例 Object.GetType 方法返回一个 Type 对象,该对象表示实例的类型。 由于所有托管类型派生自 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
let values: obj[] = [| "word"; true; 120; 136.34; 'a' |] for value in values do printfn $"{value} - type {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.GetType和Module.GetTypesModule.FindTypes方法返回
Type
表示模块中定义的类型的对象。 第一种方法可用于获取模块中定义的所有公共和专用类型的对象的数组 Type 。 (可以通过或Assembly.GetModules方法Type.Module或 property.) 获取实例Module
Assembly.GetModule该System.Reflection.Assembly对象包含许多方法,用于检索程序集中定义的类,包括Assembly.GetType和Assembly.GetTypesAssembly.GetExportedTypes。
该方法 FindInterfaces 返回类型支持的接口类型的筛选列表。
该方法 GetElementType 返回一个
Type
表示元素的对象。和GetInterfacesGetInterface方法返回Type表示类型支持的接口类型的对象。
该方法 GetTypeArray 返回一个对象数组 Type ,表示由任意对象集指定的类型。 使用类型 Object数组指定对象。
GetTypeFromProgID提供 COM 互操作性的方法和GetTypeFromCLSID方法。 返回一个 Type 对象,该对象代表由
ProgID
或指定的CLSID
类型。该方法 GetTypeFromHandle 用于互操作性。 它返回一个
Type
对象,该对象表示类句柄指定的类型。C#
typeof
运算符、C++typeid
运算符和Visual BasicGetType
运算符获取Type
类型的对象。该方法MakeGenericType返回一个Type对象,该对象表示构造的泛型类型,如果其属性返回
true
,则返回ContainsGenericParameters一个开放式构造类型;否则返回封闭构造类型。 仅当泛型类型已关闭时,才能实例化它。MakeByRefType MakePointerType和MakeArrayType方法返回对象,这些对象分别表示Type指定类型的数组、指向指定类型的指针,以及Visual Basic) 中 C#
ByRef
中“ref
byref” (的引用参数的类型。
比较类型对象是否相等
表示类型的对象是唯一 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
let number1 = 1635429L
let number2 = 16203
let number3 = 1639.41
let number4 = 193685412L
// Get the type of number1.
let t = number1.GetType()
// Compare types of all objects with number1.
printfn $"Type of number1 and number2 are equal: {Object.ReferenceEquals(t, number2.GetType())}"
printfn $"Type of number1 and number3 are equal: {Object.ReferenceEquals(t, number3.GetType())}"
printfn $"Type of number1 and number4 are equal: {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 |
表示用在特性上的成员筛选器。 此字段为只读。 |
FilterName |
表示用于名称的区分大小写的成员筛选器。 此字段为只读。 |
FilterNameIgnoreCase |
表示用于名称的不区分大小写的成员筛选器。 此字段为只读。 |
Missing |
表示 Type 信息中的缺少值。 此字段为只读。 |
属性
Assembly | |
AssemblyQualifiedName |
获取类型的程序集限定名,其中包括从中加载 Type 的程序集的名称。 |
Attributes |
获取与 Type 关联的属性。 |
BaseType |
获取当前 Type 直接从中继承的类型。 |
ContainsGenericParameters |
获取一个值,该值指示当前 Type 对象是否具有尚未被特定类型替代的类型参数。 |
CustomAttributes |
获取包含此成员自定义属性的集合。 (继承自 MemberInfo) |
DeclaringMethod |
获取一个表示声明方法的 MethodBase(如果当前 Type 表示泛型方法的一个类型参数)。 |
DeclaringType |
获取用来声明当前的嵌套类型或泛型类型参数的类型。 |
DefaultBinder |
获取默认联编程序的引用,该程序实现的内部规则用于选择由 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 |
获取指示当前类型的字段是否由公共语言运行时自动放置的值。 |
IsByRef |
获取一个值,该值指示 Type 是否由引用传递。 |
IsByRefLike |
获取一个值,该值指示类型是否是与 byref 类似的结构。 |
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 属性,如果应用了该属性,则表示它是从 COM 类型库导入的。 |
IsInterface |
获取一个值,通过该值指示 Type 是否是一个接口;即,不是类或值类型。 |
IsLayoutSequential |
获取指示当前类型的字段是否按顺序(定义顺序或发送到元数据的顺序)放置的值。 |
IsMarshalByRef |
获取一个值,该值指示 Type 是否按引用进行封送。 |
IsNested |
获取一个指示当前 Type 对象是否表示其定义嵌套在另一个类型的定义之内的类型的值。 |
IsNestedAssembly |
获取一个值,通过该值指示 Type 是否是嵌套的并且只能在它自己的程序集内可见。 |
IsNestedFamANDAssem |
获取一个值,通过该值指示 Type 是否是嵌套的并且只对同时属于自己家族和自己程序集的类可见。 |
IsNestedFamily |
获取一个值,通过该值指示 Type 是否是嵌套的并且只能在它自己的家族内可见。 |
IsNestedFamORAssem |
获取一个值,通过该值指示 Type 是否是嵌套的并且只对属于它自己的家族或属于它自己的程序集的类可见。 |
IsNestedPrivate |
获取一个值,通过该值指示 Type 是否是嵌套的并声明为私有。 |
IsNestedPublic |
获取一个值,通过该值指示类是否是嵌套的并且声明为公共的。 |
IsNotPublic |
获取一个值,该值指示 Type 是否声明为公共类型。 |
IsPointer |
获取一个值,通过该值指示 Type 是否为指针。 |
IsPrimitive |
获取一个值,通过该值指示 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 |
指示表示该类型的公共语言运行时提供的类型。 |
方法
运算符
Equality(Type, Type) |
指示两个 Type 对象是否相等。 |
Inequality(Type, Type) |
指示两个 Type 对象是否不相等。 |
显式接口实现
_MemberInfo.GetIDsOfNames(Guid, IntPtr, UInt32, UInt32, IntPtr) |
将一组名称映射为对应的一组调度标识符。 (继承自 MemberInfo) |
_MemberInfo.GetType() |
获取一个表示 MemberInfo 类的 Type 对象。 (继承自 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) |
将一组名称映射为对应的一组调度标识符。 |
_Type.GetTypeInfo(UInt32, UInt32, IntPtr) |
检索对象的类型信息,然后可以使用该信息获取接口的类型信息。 |
_Type.GetTypeInfoCount(UInt32) |
检索对象提供的类型信息接口的数量(0 或 1)。 |
_Type.Invoke(UInt32, Guid, UInt32, Int16, IntPtr, IntPtr, IntPtr, IntPtr) |
提供对某一对象公开的属性和方法的访问。 |
扩展方法
适用于
线程安全性
此类型是线程安全的。