Type 类
表示类型声明:类类型、接口类型、数组类型、值类型、枚举类型、类型参数、泛型类型定义,以及开放或封闭构造的泛型类型。( Classes, Value types, Arrays, Interfaces, Enumerations, Delegates, Constructed generic types and generic type definitions, Type arguments and type parameters of constructed generic types, generic type definitions, and generic method definitions)
System.Object
System.Reflection.MemberInfo
System.Type
System.Reflection.TypeInfo
语法:
[SerializableAttribute] [ClassInterfaceAttribute(ClassInterfaceType.None)] [ComVisibleAttribute(true)]public abstract class Type : MemberInfo, _Type, IReflect
名称 | 说明 | |
---|---|---|
Type() | 初始化 Type 类的新实例。 |
名称 | 说明 | |
---|---|---|
Assembly | ||
AssemblyQualifiedName | 获取类型的程序集限定名,其中包括从中加载 Type 的程序集的名称。 | |
Attributes | 获取与 Type 关联的属性。 | |
BaseType | 获取当前 Type 直接从中继承的类型。 | |
ContainsGenericParameters | 获取一个值,该值指示当前 Type 对象是否具有尚未被特定类型替代的类型参数。 | |
CustomAttributes | 获取包含此成员的自定义特性的集合。(继承自 MemberInfo。) | |
DeclaringMethod | 获取一个表示声明方法的 MethodBase(如果当前 Type 表示泛型方法的一个类型参数)。 | |
DeclaringType | 获取用来声明当前的嵌套类型或泛型类型参数的类型。(覆盖 MemberInfo.DeclaringType。) | |
DefaultBinder | 获取默认联编程序的引用,该程序实现的内部规则用于选择由 InvokeMember 调用的合适成员。 | |
FullName | 获取该类型的完全限定名称,包括其命名空间,但不包括程序集。 | |
GenericParameterAttributes | 获取描述当前泛型类型参数的协变和特殊约束的 GenericParameterAttributes 标志。 | |
GenericParameterPosition | 当 Type 对象表示泛型类型或泛型方法的类型参数时,获取类型参数在声明它的泛型类型或方法的类型参数列表中的位置。 | |
GenericTypeArguments | 获取此类型泛型类型参数的数组。 | |
GUID | 获取与 Type 关联的 GUID。 | |
HasElementType | 获取一个值,通过该值指示当前 Type 是包含还是引用另一类型,即当前 Type 是数组、指针还是通过引用传递。 | |
IsAbstract | 获取一个值,通过该值指示 Type 是否为抽象的并且必须被重写。 | |
IsAnsiClass | 获取一个值,该值指示是否为 AnsiClass 选择了字符串格式属性 Type。 | |
IsArray | 获取一个值,该值指示类型是否为数组。 | |
IsAutoClass | 获取一个值,该值指示是否为 AutoClass 选择了字符串格式属性 Type。 | |
IsAutoLayout | 获取指示当前类型的字段是否由公共语言运行时自动放置的值。 | |
IsByRef | 获取一个值,该值指示 Type 是否由引用传递。 | |
IsClass | 获取一个值,通过该值指示 Type 是否是一个类或委托;即,不是值类型或接口。 | |
IsCOMObject | 获取一个值,通过该值指示 Type 是否为 COM 对象。 | |
IsConstructedGenericType | 获取指示此对象是否表示构造的泛型类型的值。 你可以创建构造型泛型类型的实例。 | |
IsContextful | 获取一个值,通过该值指示 Type 在上下文中是否可以被承载。 | |
IsEnum | 获取一个值,该值指示当前的 Type 是否表示枚举。 | |
IsExplicitLayout | 获取指示当前类型的字段是否放置在显式指定的偏移量处的值。 | |
IsGenericParameter | 获取一个值,该值指示当前 Type 是否表示泛型类型或方法的定义中的类型参数。 | |
IsGenericType | 获取一个值,该值指示当前类型是否是泛型类型。 | |
IsGenericTypeDefinition | 获取一个值,该值指示当前 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 是否为可序列化的。 | |
IsSpecialName | 获取一个值,该值指示该类型是否具有需要特殊处理的名称。 | |
IsUnicodeClass | 获取一个值,该值指示是否为 UnicodeClass 选择了字符串格式属性 Type。 | |
IsValueType | 获取一个值,通过该值指示 Type 是否为值类型。 | |
IsVisible | 获取一个指示 Type 是否可由程序集之外的代码访问的值。 | |
MemberType | 获取一个指示此成员是类型还是嵌套类型的 MemberTypes 值。(覆盖 MemberInfo.MemberType。) | |
MetadataToken | 获取一个值,标识元数据中的元素。(继承自 MemberInfo。) | |
Module | 获取在其中定义当前 Type 的模块 (DLL)。 | |
Name | 获取当前成员的名称。(继承自 MemberInfo。) | |
Namespace | 获取 Type 的命名空间。 | |
ReflectedType | 获取用于获取该成员的类对象。(覆盖 MemberInfo.ReflectedType。) | |
StructLayoutAttribute | 获取一个描述当前类型的布局的 StructLayoutAttribute。 | |
TypeHandle | 获取当前 Type 的句柄。 | |
TypeInitializer | 获取该类型的初始值设定项。 | |
UnderlyingSystemType | 指示表示该类型的公共语言运行时提供的类型。 |
名称 | 说明 | |
---|---|---|
Delimiter | 分隔 Type 的命名空间中的名称。 此字段为只读。 | |
EmptyTypes | 表示 Type 类型的空数组。 此字段为只读。 | |
FilterAttribute | 表示用在特性上的成员筛选器。 此字段为只读。 | |
FilterName | 表示用于名称的区分大小写的成员筛选器。 此字段为只读。 | |
FilterNameIgnoreCase | 表示用于名称的不区分大小写的成员筛选器。 此字段为只读。 | |
Missing | 表示 Type 信息中的缺少值。 此字段为只读。 |
名称 | 说明 | |
---|---|---|
Equality(Type,Type) | 指示两个 Type 对象是否相等。 | |
Inequality(Type,Type) | 指示两个 Type 对象是否不相等。 |
名称 | 说明 | |
---|---|---|
_MemberInfo.GetIDsOfNames(Guid,IntPtr,UInt32,UInt32,IntPtr) | 将一组名称映射为对应的一组调度标识符。(继承自 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) | 将一组名称映射为对应的一组调度标识符。 | |
_Type.GetTypeInfo(UInt32,UInt32,IntPtr) | 检索对象的类型信息,然后可以使用该信息获取接口的类型信息。 | |
_Type.GetTypeInfoCount(UInt32) | 检索对象提供的类型信息接口的数量(0 或 1)。 | |
_Type.Invoke(UInt32,Guid,UInt32,Int16,IntPtr,IntPtr,IntPtr,IntPtr) | 提供对某一对象公开的属性和方法的访问。 |
说明 |
---|
To view the .NET Framework source code for this type, see the Reference Sourcehttp://referencesource.microsoft.com/#mscorlib/system/type.cs#3d00eeab9feb80f3. You can browse through the source code online, download the reference for offline viewing, and step through the sources (including patches and updates) during debugging; see instructionshttp://referencesource.microsoft.com/. |
Type is the root of the System.Reflection functionality and is the primary way to access metadata. Use the members of Type to get information about a type declaration, about the members of a type (such as the constructors, methods, fields, properties, and events of a class), as well as the module and the assembly in which the class is deployed.
No permissions are required for code to use reflection to get information about types and their members, regardless of their access levels. No permissions are required for code to use reflection to access public members, or other members whose access levels would make them visible during normal compilation. However, in order for your code to use reflection to access members that would normally be inaccessible, such as private or internal methods, or protected fields of a type your class does not inherit, your code must have T:System.Security.Permissions.ReflectionPermission. See Security Considerations for Reflection.
Type is an abstract base class that allows multiple implementations. The system will always provide the derived class RuntimeType. In reflection, all classes beginning with the word Runtime are created only once per object in the system and support comparison operations.
说明 |
---|
In multithreading scenarios, do not lock T:System.Type objects in order to synchronize access to static data. Other code, over which you have no control, might also lock your class type. This might result in a deadlock. Instead, synchronize access to static data by locking a private static object. |
说明 |
---|
A derived class can access protected members of the calling code's base classes. Also, access is allowed to assembly members of the calling code's assembly. As a rule, if you are allowed access in early-bound code, then you are also allowed access in late-bound code. |
说明 |
---|
Interfaces that extend other interfaces do not inherit the methods defined in the extended interfaces. |
What types does a Type object represent?
This class is thread safe; multiple threads can concurrently read from an instance of this type. An instance of the T:System.Type class can represent any of the following types:
Classes
Value types
Arrays
Interfaces
Enumerations
Delegates
Constructed generic types and generic type definitions
Type arguments and type parameters of constructed generic types, generic type definitions, and generic method definitions
The T:System.Type object associated with a particular type can be obtained in the following ways:
The instance M:System.Object.GetType method returns a T:System.Type object that represents the type of an instance. Because all managed types derive from T:System.Object, the M:System.Object.GetType method can be called on an instance of any type.
The following example calls the M:System.Object.GetType method to determine the runtime type of each object in an object array.
using System;public class Example { public static void Main() { 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
The static M:System.Type.GetType(System.String) methods return a T:System.Type object that represents a type specified by its fully qualified name.
The M:System.Reflection.Module.GetTypes, M:System.Reflection.Module.GetType(System.String,System.Boolean), and M:System.Reflection.Module.FindTypes(System.Reflection.TypeFilter,System.Object) methods return Type objects that represent the types defined in a module. The first method can be used to obtain an array of T:System.Type objects for all the public and private types defined in a module. (You can obtain an instance of Module through the M:System.Reflection.Assembly.GetModule(System.String) or M:System.Reflection.Assembly.GetModules method, or through the P:System.Type.Module property.)
The T:System.Reflection.Assembly object contains a number of methods to retrieve the classes defined in an assembly, including Overload:System.Reflection.Assembly.GetType, M:System.Reflection.Assembly.GetTypes, and M:System.Reflection.Assembly.GetExportedTypes.
The M:System.Type.FindInterfaces(System.Reflection.TypeFilter,System.Object) method returns a filtered list of interface types supported by a type.
The M:System.Type.GetElementType method returns a Type object that represents the element.
The M:System.Type.GetInterfaces and M:System.Type.GetInterface(System.String) methods return T:System.Type objects representing the interface types supported by a type.
The M:System.Type.GetTypeArray(System.Object[]) method returns an array of T:System.Type objects representing the types specified by an arbitrary set of objects. The objects are specified with an array of type T:System.Object.
The M:System.Type.GetTypeFromProgID(System.String) and M:System.Type.GetTypeFromCLSID(System.Guid) methods are provided for COM interoperability. They return a T:System.Type object that represents the type specified by a ProgID or CLSID.
The M:System.Type.GetTypeFromHandle(System.RuntimeTypeHandle) method is provided for interoperability. It returns a Type object that represents the type specified by a class handle.
The C# typeof operator, the C++ typeid operator, and the Visual Basic GetType operator obtain the Type object for a type.
The M:System.Type.MakeGenericType(System.Type[]) method returns a T:System.Type object representing a constructed generic type, which is an open constructed type if its P:System.Type.ContainsGenericParameters property returns true, and a closed constructed type otherwise. A generic type can be instantiated only if it is closed.
The Overload:System.Type.MakeArrayType, M:System.Type.MakePointerType, and M:System.Type.MakeByRefType methods return T:System.Type objects that represent, respectively, an array of a specified type, a pointer to a specified type, and the type of a reference parameter (ref in C#, ByRef in Visual Basic).
Comparing type objects for equality
A T:System.Type object that represents a type is unique; that is, two T:System.Type object references refer to the same object if and only if they represent the same type. This allows for comparison of T:System.Type objects using reference equality. The following example compares the T:System.Type objects that represent a number of integer values to determine whether they are of the same type.
using System;public class Example { public static void Main() { 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
继承函数说明:
When you inherit from Type, you must override the following members:
The following example shows a few representative features of T:System.Type. The C# typeof operator (GetType operator in Visual Basic, typeid operator in Visual C++) is used to get a T:System.Type object representing T:System.String. From this T:System.Type object, the Overload:System.Type.GetMethod method is used to get a T:System.Reflection.MethodInfo representing the M:System.String.Substring(System.Int32,System.Int32) overload that takes a starting location and a length.
To identify the overload signature, the code example creates a temporary array containing two T:System.Type objects representing int (Integer in Visual Basic).
说明 |
---|
To be precise, the array contains two references to the instance of T:System.Type that represents int in the current application domain. For any type, there is only one instance of T:System.Type per application domain. |
The code example uses the T:System.Reflection.MethodInfo to invoke the M:System.String.Substring(System.Int32,System.Int32) method on the string "Hello, World!", and displays the result.
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". */
备注:转自https://msdn.microsoft.com/zh-cn/library/system.type(v=vs.110).aspx
转载于:https://blog.51cto.com/jiaojusuimu/1880018