指定完全限定的类型名称Specifying fully qualified type names

必须指定类型名称才能为各种反射操作提供有效输入。You must specify type names to have valid input to various reflection operations. 完全限定的类型名称包含程序集名称规范、命名空间规范和类型名称。A fully qualified type name consists of an assembly name specification, a namespace specification, and a type name. 类型名称规范由 Type.GetTypeModule.GetTypeModuleBuilder.GetTypeAssembly.GetType 等方法使用。Type name specifications are used by methods such as Type.GetType, Module.GetType, ModuleBuilder.GetType, and Assembly.GetType.

类型名称的语法Grammar for type names

语法定义正式语言的语法。The grammar defines the syntax of formal languages. 下表列出了词法规则,说明如何识别有效输入。The following table lists lexical rules that describe how to recognize a valid input. 最终元素(无法再减小的元素)全部以大写字母显示。Terminals (those elements that are not further reducible) are shown in all uppercase letters. 非最终元素(可以再减小的元素)则显示为大小写混合或带单引号的字符串,但单引号 (') 不是语法本身的一部分。Nonterminals (those elements that are further reducible) are shown in mixed-case or singly quoted strings, but the single quote (') is not a part of the syntax itself. 竖线 (|) 表示具有子规则的规则。The pipe character (|) denotes rules that have subrules.

TypeSpec
    : ReferenceTypeSpec
    | SimpleTypeSpec
    ;

ReferenceTypeSpec
    : SimpleTypeSpec '&'
    ;

SimpleTypeSpec
    : PointerTypeSpec
    | GenericTypeSpec
    | TypeName
    ;

GenericTypeSpec
   : SimpleTypeSpec ` NUMBER

PointerTypeSpec
    : SimpleTypeSpec '*'
    ;

ArrayTypeSpec
    : SimpleTypeSpec '[ReflectionDimension]'
    | SimpleTypeSpec '[ReflectionEmitDimension]'
    ;

ReflectionDimension
    : '*'
    | ReflectionDimension ',' ReflectionDimension
    | NOTOKEN
    ;

ReflectionEmitDimension
    : '*'
    | Number '..' Number
    | Number '…'
    | ReflectionDimension ',' ReflectionDimension
    | NOTOKEN
    ;

Number
    : [0-9]+
    ;

TypeName
    : NamespaceTypeName
    | NamespaceTypeName ',' AssemblyNameSpec
    ;

NamespaceTypeName
    : NestedTypeName
    | NamespaceSpec '.' NestedTypeName
    ;

NestedTypeName
    : IDENTIFIER
    | NestedTypeName '+' IDENTIFIER
    ;

NamespaceSpec
    : IDENTIFIER
    | NamespaceSpec '.' IDENTIFIER
    ;

AssemblyNameSpec
    : IDENTIFIER
    | IDENTIFIER ',' AssemblyProperties
    ;

AssemblyProperties
    : AssemblyProperty
    | AssemblyProperties ',' AssemblyProperty
    ;

AssemblyProperty
    : AssemblyPropertyName '=' AssemblyPropertyValue
    ;

指定特殊字符Specifying special characters

在类型名称中,IDENTIFIER 是语言规则确定的任何有效名称。In a type name, IDENTIFIER is any valid name determined by the rules of a language.

反斜杠 (\) 可用作转义符来分隔以下用作 IDENTIFIER 一部分的标记。Use the backslash (\) as an escape character to separate the following tokens when used as part of IDENTIFIER.

标记Token 含义Meaning
\,\, 程序集分隔符。Assembly separator.
\+ 嵌套类型分隔符。Nested type separator.
\& 引用类型。Reference type.
\* 指针类型。Pointer type.
\[\[ 数组维度分隔符。Array dimension delimiter.
\]\] 数组维度分隔符。Array dimension delimiter.
\\. 只有在数组规范中使用句点时,才应在句点前使用反斜杠。Use the backslash before a period only if the period is used in an array specification. NamespaceSpec 中的句点不使用反斜杠。Periods in NamespaceSpec do not take the backslash.
\|必要时将反斜杠用作字符串文本。\|Backslash when needed as a string literal.

请注意,除 AssemblyNameSpec 外,所有 TypeSpec 组件中的空格都是相关的。Note that in all TypeSpec components except AssemblyNameSpec, spaces are relevant. 在 AssemblyNameSpec 中,“,”分隔符之前的空格相关,但“,”分隔符之后的空格将被忽略。In the AssemblyNameSpec, spaces before the ',' separator are relevant, but spaces after the ',' separator are ignored.

反射类(如 Type.FullName)返回改变后的名称,以便使返回的名称可以用在对 GetType 的调用中(如同用在 MyType.GetType(myType.FullName) 中)。Reflection classes, such as Type.FullName, return the mangled name so that the returned name can be used in a call to GetType, as in MyType.GetType(myType.FullName).

例如,某个类型的完全限定名称可能是 Ozzy.OutBack.Kangaroo+Wallaby,MyAssemblyFor example, the fully qualified name for a type might be Ozzy.OutBack.Kangaroo+Wallaby,MyAssembly.

如果命名空间为 Ozzy.Out+Back,则必须在加号前加反斜杠。If the namespace were Ozzy.Out+Back, then the plus sign must be preceded by a backslash. 否则,分析器会将其解释为嵌套分隔符。Otherwise, the parser would interpret it as a nesting separator. 反射会将该字符串当作 Ozzy.Out\+Back.Kangaroo+Wallaby,MyAssembly 发出。Reflection emits this string as Ozzy.Out\+Back.Kangaroo+Wallaby,MyAssembly.

指定程序集名称Specifying assembly names

程序集名称规范中至少需要具有程序集的文本名称 (IDENTIFIER)。The minimum information required in an assembly name specification is the textual name (IDENTIFIER) of the assembly. 可以在 IDENTIFIER 后添加下表所述的以逗号分隔的属性/值对列表。You can follow the IDENTIFIER by a comma-separated list of property/value pairs as described in the following table. IDENTIFIER 命名应遵循文件命名的规则。IDENTIFIER naming should follow the rules for file naming. IDENTIFIER 不区分大小写。The IDENTIFIER is case-insensitive.

属性名称Property name 描述Description 允许的值Allowable values
VersionVersion 程序集版本号Assembly version number Major.Minor.Build.Revision,其中 Major、Minor、Build 和 Revision 是 0 和 65535 之间(含 0 和 65535)的整数。Major.Minor.Build.Revision, where Major, Minor, Build, and Revision are integers between 0 and 65535 inclusive.
PublicKeyPublicKey 完整公钥Full public key 完整公钥十六进制格式的字符串值。String value of full public key in hexadecimal format. 指定 null 引用(在 Visual Basic 中为 Nothing)以显式指示私有程序集。Specify a null reference (Nothing in Visual Basic) to explicitly indicate a private assembly.
PublicKeyTokenPublicKeyToken 公钥标记(完整公钥的 8 字节哈希)Public key token (8-byte hash of the full public key) 公钥标记十六进制格式的字符串值。String value of public key token in hexadecimal format. 指定 null 引用(在 Visual Basic 中为 Nothing)以显式指示私有程序集。Specify a null reference (Nothing in Visual Basic) to explicitly indicate a private assembly.
区域性Culture 程序集区域性Assembly culture RFC-1766 格式的程序集区域性,对于独立于语言(非附属)的程序集则为“非特定”。Culture of the assembly in RFC-1766 format, or "neutral" for language-independent (nonsatellite) assemblies.
自定义Custom 自定义二进制大对象 (BLOB)。Custom binary large object (BLOB). 它当前仅用于由本机图像生成器 (Ngen) 生成的程序集。This is currently used only in assemblies generated by the Native Image Generator (Ngen). 本机图像生成器工具用以向程序集缓存通知所安装程序集为本机图像的自定义字符串,因此该自定义字符串安装在本机图像缓存中。Custom string used by the Native Image Generator tool to notify the assembly cache that the assembly being installed is a native image, and is therefore to be installed in the native image cache. 也称作 zap 字符串。Also called a zap string.

下面的示例演示具有默认区域性的简单命名程序集的 AssemblyName。The following example shows an AssemblyName for a simply named assembly with default culture.

com.microsoft.crypto, Culture=""

下面的示例演示区域性为“en”的强名称程序集的完全指定的引用。The following example shows a fully specified reference for a strongly named assembly with culture "en".

com.microsoft.crypto, Culture=en, PublicKeyToken=a5d015c7d5a0b012,
    Version=1.0.0.0

下面的示例演示部分指定的 AssemblyName,它可以由具有强名称或简单名称的程序集来满足。The following examples each show a partially specified AssemblyName, which can be satisfied by either a strong or a simply named assembly.

com.microsoft.crypto
com.microsoft.crypto, Culture=""
com.microsoft.crypto, Culture=en

下面的示例分别演示一个部分指定的 AssemblyName,它必须由具有简单名称的程序集来满足。The following examples each show a partially specified AssemblyName, which must be satisfied by a simply named assembly.

com.microsoft.crypto, Culture="", PublicKeyToken=null
com.microsoft.crypto, Culture=en, PublicKeyToken=null

下面的示例分别演示一个部分指定的 AssemblyName,它必须由具有强名称的程序集来满足。The following examples each show a partially specified AssemblyName, which must be satisfied by a strongly named assembly.

com.microsoft.crypto, Culture="", PublicKeyToken=a5d015c7d5a0b012
com.microsoft.crypto, Culture=en, PublicKeyToken=a5d015c7d5a0b012,
    Version=1.0.0.0

指定泛型类型Specifying generic types

SimpleTypeSpec`NUMBER 表示具有从 1 到 n 泛型类型参数的开放式泛型类型。SimpleTypeSpec`NUMBER represents an open generic type with from 1 to n generic type parameters. 例如,若要获取对开放式泛型类型 List<T> 或封闭式泛型类型 List<String> 的引用,请使用 Type.GetType("System.Collections.Generic.List`1") 若要获取对泛型类型 Dictionary<TKey,TValue> 的引用,请使用 Type.GetType("System.Collections.Generic.Dictionary`2")For example, to get reference to the open generic type List<T> or the closed generic type List<String>, use Type.GetType("System.Collections.Generic.List`1") To get a reference to the generic type Dictionary<TKey,TValue>, use Type.GetType("System.Collections.Generic.Dictionary`2").

指定指针Specifying pointers

SimpleTypeSpec* 表示未托管的指针。SimpleTypeSpec* represents an unmanaged pointer. 例如,要获取指向 MyType 类型的指针,请使用 Type.GetType("MyType*")For example, to get a pointer to type MyType, use Type.GetType("MyType*"). 要获取指向 MyType 类型指针的指针,请使用 Type.GetType("MyType**")To get a pointer to a pointer to type MyType, use Type.GetType("MyType**").

指定引用Specifying references

SimpleTypeSpec & 表示托管指针或引用。SimpleTypeSpec & represents a managed pointer or reference. 例如,要获取对 MyType 类型的引用,请使用 Type.GetType("MyType &")For example, to get a reference to type MyType, use Type.GetType("MyType &"). 请注意,与指针不同,引用仅限于一个级别。Note that unlike pointers, references are limited to one level.

指定数组Specifying arrays

在 BNF 语法中,ReflectionEmitDimension 仅适用于使用 ModuleBuilder.GetType 检索的不完整类型定义。In the BNF Grammar, ReflectionEmitDimension only applies to incomplete type definitions retrieved using ModuleBuilder.GetType. 不完整类型定义是使用 System.Reflection.Emit 构造但没有对其调用 TypeBuilder.CreateTypeTypeBuilder 对象。Incomplete type definitions are TypeBuilder objects constructed using System.Reflection.Emit but on which TypeBuilder.CreateType has not been called. ReflectionDimension 可用于检索任何已完成的类型定义,即已加载的类型。ReflectionDimension can be used to retrieve any type definition that has been completed, that is, a type that has been loaded.

通过指定数组的级别,可以访问反射中的数组:Arrays are accessed in reflection by specifying the rank of the array:

  • Type.GetType("MyArray[]") 获取下限为 0 的单维数组。Type.GetType("MyArray[]") gets a single-dimension array with 0 lower bound.

  • Type.GetType("MyArray[*]") 获取下限未知的单维数组。Type.GetType("MyArray[*]") gets a single-dimension array with unknown lower bound.

  • Type.GetType("MyArray[][]") 获取二维数组的数组。Type.GetType("MyArray[][]") gets a two-dimensional array's array.

  • Type.GetType("MyArray[*,*]")Type.GetType("MyArray[,]") 获取下限未知的矩形二维数组。Type.GetType("MyArray[*,*]") and Type.GetType("MyArray[,]") gets a rectangular two-dimensional array with unknown lower bounds.

请注意,从运行时的角度来看,MyArray[] != MyArray[*],但对于多维数组,这两种表示是等效的。Note that from a runtime point of view, MyArray[] != MyArray[*], but for multidimensional arrays, the two notations are equivalent. 也就是说,Type.GetType("MyArray [,]") == Type.GetType("MyArray[*,*]") 的计算结果为 true。That is, Type.GetType("MyArray [,]") == Type.GetType("MyArray[*,*]") evaluates to true.

对于 ModuleBuilder.GetType,MyArray[0..5] 指示大小为 6、下限为 0 的单维数组。For ModuleBuilder.GetType, MyArray[0..5] indicates a single-dimension array with size 6, lower bound 0. MyArray[4…] 指示大小未知、下限为 4 的单维数组。MyArray[4…] indicates a single-dimension array of unknown size and lower bound 4.

请参阅See also