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 。 中的 C# typeof
(GetType
运算符Visual Basic) 获取 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 对象
比较类型对象相等性
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
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 property.)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
构造类型。 泛型类型只有在关闭时才能实例化。、 和 方法返回对象,这些对象分别表示指定类型的数组、指向指定类型的指针,以及 C# 中引用参数 (的类型 MakeArrayType MakePointerType MakeByRefType Type
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 |
表示用在特性上的成员筛选器。 此字段为只读。 |
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) |
提供对某一对象公开的属性和方法的访问。 |
扩展方法
适用于
线程安全性
此类型是线程安全的。