Type.GetType Type.GetType Type.GetType Type.GetType Method

定义

获取表示指定类型的 Type 对象。Gets a Type object that represents the specified type.

重载

GetType() GetType() GetType() GetType()

获取当前 TypeGets the current Type.

GetType(String) GetType(String) GetType(String) GetType(String)

获取具有指定名称的 Type,执行区分大小写的搜索。Gets the Type with the specified name, performing a case-sensitive search.

GetType(String, Boolean) GetType(String, Boolean) GetType(String, Boolean) GetType(String, Boolean)

获取具有指定名称的 Type,指定是否执行区分大小写的搜索,以及在找不到类型时是否引发异常。Gets the Type with the specified name, performing a case-sensitive search and specifying whether to throw an exception if the type is not found.

GetType(String, Boolean, Boolean) GetType(String, Boolean, Boolean) GetType(String, Boolean, Boolean) GetType(String, Boolean, Boolean)

获取具有指定名称的 Type,指定是否执行区分大小写的搜索,以及在找不到类型时是否引发异常。Gets the Type with the specified name, specifying whether to throw an exception if the type is not found and whether to perform a case-sensitive search.

GetType(String, Func<AssemblyName,Assembly>, Func<Assembly,String,Boolean,Type>) GetType(String, Func<AssemblyName,Assembly>, Func<Assembly,String,Boolean,Type>) GetType(String, Func<AssemblyName,Assembly>, Func<Assembly,String,Boolean,Type>) GetType(String, Func<AssemblyName,Assembly>, Func<Assembly,String,Boolean,Type>)

获取具有指定名称的类型,(可选)提供自定义方法以解析程序集和该类型。Gets the type with the specified name, optionally providing custom methods to resolve the assembly and the type.

GetType(String, Func<AssemblyName,Assembly>, Func<Assembly,String,Boolean,Type>, Boolean) GetType(String, Func<AssemblyName,Assembly>, Func<Assembly,String,Boolean,Type>, Boolean) GetType(String, Func<AssemblyName,Assembly>, Func<Assembly,String,Boolean,Type>, Boolean) GetType(String, Func<AssemblyName,Assembly>, Func<Assembly,String,Boolean,Type>, Boolean)

获取具有指定名称的类型,指定在找不到该类型时是否引发异常,(可选)提供自定义方法以解析程序集和该类型。Gets the type with the specified name, specifying whether to throw an exception if the type is not found, and optionally providing custom methods to resolve the assembly and the type.

GetType(String, Func<AssemblyName,Assembly>, Func<Assembly,String,Boolean,Type>, Boolean, Boolean) GetType(String, Func<AssemblyName,Assembly>, Func<Assembly,String,Boolean,Type>, Boolean, Boolean) GetType(String, Func<AssemblyName,Assembly>, Func<Assembly,String,Boolean,Type>, Boolean, Boolean) GetType(String, Func<AssemblyName,Assembly>, Func<Assembly,String,Boolean,Type>, Boolean, Boolean)

获取具有指定名称的类型,指定是否执行区分大小写的搜索,在找不到类型时是否引发异常,(可选)提供自定义方法以解析程序集和该类型。Gets the type with the specified name, specifying whether to perform a case-sensitive search and whether to throw an exception if the type is not found, and optionally providing custom methods to resolve the assembly and the type.

GetType() GetType() GetType() GetType()

获取当前 TypeGets the current Type.

public:
 virtual Type ^ GetType();
public Type GetType ();
override this.GetType : unit -> Type
Public Function GetType () As Type

返回

当前的 TypeThe current Type.

实现

异常

调用了类初始值设定项,且该项引发了异常。A class initializer is invoked and throws an exception.

另请参阅

GetType(String) GetType(String) GetType(String) GetType(String)

获取具有指定名称的 Type,执行区分大小写的搜索。Gets the Type with the specified name, performing a case-sensitive search.

public:
 static Type ^ GetType(System::String ^ typeName);
public static Type GetType (string typeName);
static member GetType : string -> Type
Public Shared Function GetType (typeName As String) As Type

参数

typeName
String String String String

要获取的类型的程序集限定名称。The assembly-qualified name of the type to get. 请参阅 AssemblyQualifiedNameSee AssemblyQualifiedName. 如果该类型位于当前正在执行的程序集中或者 Mscorlib.dll 中,则提供由命名空间限定的类型名称就足够了。If the type is in the currently executing assembly or in Mscorlib.dll, it is sufficient to supply the type name qualified by its namespace.

返回

具有指定名称的类型(如果找到的话);否则为 nullThe type with the specified name, if found; otherwise, null.

异常

调用了类初始值设定项,且该项引发了异常。A class initializer is invoked and throws an exception.

typeName 表示将指针类型、ByRef 类型或 Void 作为其类型参数之一的泛型类型。typeName represents a generic type that has a pointer type, a ByRef type, or Void as one of its type arguments.

- 或 --or- typeName 表示具有错误的类型参数数目的泛型类型。typeName represents a generic type that has an incorrect number of type arguments.

- 或 --or- typeName 表示泛型类型,并且其类型参数之一不满足相应类型参数的约束。typeName represents a generic type, and one of its type arguments does not satisfy the constraints for the corresponding type parameter.

typeName 表示 TypedReference 的数组。typeName represents an array of TypedReference.

适用于 Windows 应用商店应用的 .NET可移植类库中,改为捕获基类异常 IOExceptionIn the .NET for Windows Store apps or the Portable Class Library, catch the base class exception, IOException, instead.

找到了程序集或其依赖项之一,但无法加载它们。The assembly or one of its dependencies was found, but could not be loaded.

程序集或其依赖项之一无效。The assembly or one of its dependencies is not valid.

- 或 --or- 当前加载的是公共语言运行时 2.0 版或更高版本,而程序集使用更高的版本编译。Version 2.0 or later of the common language runtime is currently loaded, and the assembly was compiled with a later version.

示例

下面的示例检索的类型System.Int32,并使用该类型对象以显示FullName属性的System.Int32The following example retrieves the type of System.Int32 and uses that type object to display the FullName property of System.Int32.

using namespace System;

int main()
{
   try {
      // Get the type of a specified class.
      Type^ myType1 = Type::GetType( "System.Int32" );
      Console::WriteLine( "The full name is {0}.\n", myType1->FullName );
   }
   catch ( TypeLoadException^ e ) {
       Console::WriteLine("{0}: Unable to load type System.Int32",
                          e->GetType()->Name);
   }

   try {
      // Since NoneSuch does not exist in this assembly, GetType throws a TypeLoadException.
      Type^ myType2 = Type::GetType( "NoneSuch", true );
      Console::WriteLine( "The full name is {0}.", myType2->FullName );
   }
   catch ( TypeLoadException^ e ) {
       Console::WriteLine("{0}: Unable to load type NoneSuch",
                          e->GetType()->Name);
   }

}
// The example displays the following output:
//       The full name is System.Int32.
//
//       TypeLoadException: Unable to load type NoneSuch
using System;

class Example
{
     public static void Main()
     {
         try {
             // Get the type of a specified class.
             Type myType1 = Type.GetType("System.Int32");
             Console.WriteLine("The full name is {0}.\n", myType1.FullName);
         }
         catch (TypeLoadException e)
         {
            Console.WriteLine("{0}: Unable to load type System.Int32", e.GetType().Name);
         }

         try {
             // Since NoneSuch does not exist in this assembly, GetType throws a TypeLoadException.
             Type myType2 = Type.GetType("NoneSuch", true);
             Console.WriteLine("The full name is {0}.", myType2.FullName);
         }
         catch(TypeLoadException e) {
            Console.WriteLine("{0}: Unable to load type NoneSuch", e.GetType().Name);
         }
     }
}
// The example displays the following output:
//       The full name is System.Int32.
//
//       TypeLoadException: Unable to load type NoneSuch
Class Example
     Public Shared Sub Main()
         Try
             ' Get the type of the specified class.
             Dim myType1 As Type = Type.GetType("System.Int32")
             Console.WriteLine("The full name is {0}.", myType1.FullName)
         Catch e As TypeLoadException
             Console.WriteLine("{0}: Unable to load type System.Int32",
                               e.GetType().Name)
         End Try

         Console.WriteLine()

         Try
             ' Since NoneSuch does not exist in this assembly, GetType throws a TypeLoadException.
             Dim myType2 As Type = Type.GetType("NoneSuch", True)
             Console.WriteLine("The full name is {0}.", myType2.FullName)
         Catch e As TypeLoadException
             Console.WriteLine("{0}: Unable to load type NoneSuch", e.GetType().Name)
         End Try
     End Sub
End Class
' The example displays the following output:
'       The full name is System.Int32.
'
'       TypeLoadException: Unable to load type NoneSuch

注解

可以使用GetType方法来获取Type如果你知道可以从获取其程序集限定名称的另一个程序集中的类型的对象AssemblyQualifiedNameYou can use the GetType method to obtain a Type object for a type in another assembly if you know its assembly-qualified name, which can be obtained from AssemblyQualifiedName. GetType 导致在指定的程序集加载typeNameGetType causes loading of the assembly specified in typeName. 您也可以加载使用程序集Assembly.Load方法,并使用Assembly.GetTypeAssembly.GetTypes方法以获取Type对象。You can also load an assembly using the Assembly.Load method, and then use the Assembly.GetType or Assembly.GetTypes method to get Type objects. 如果类型是在编译时已知到您的程序的程序集中,则使用更加高效typeof在C#或GetType在 Visual Basic 中的运算符。If a type is in an assembly known to your program at compile time, it is more efficient to use typeof in C# or the GetType operator in Visual Basic.

备注

如果typeName找不到,在调用GetType(String)方法将返回nullIf typeName cannot be found, the call to the GetType(String) method returns null. 它不会引发异常。It does not throw an exception. 若要控制是否会引发异常,调用的重载GetType方法具有throwOnError参数。To control whether an exception is thrown, call an overload of the GetType method that has a throwOnError parameter.

GetType 仅适用于从磁盘加载的程序集。GetType only works on assemblies loaded from disk. 如果您调用GetType来查找使用定义动态程序集中定义的类型System.Reflection.Emit服务,你可能会收到不一致的行为。If you call GetType to look up a type defined in a dynamic assembly defined using the System.Reflection.Emit services, you might get inconsistent behavior. 行为取决于动态程序集是持久的也就是说,创建使用RunAndSaveSave访问模式的System.Reflection.Emit.AssemblyBuilderAccess枚举。The behavior depends on whether the dynamic assembly is persistent, that is, created using the RunAndSave or Save access modes of the System.Reflection.Emit.AssemblyBuilderAccess enumeration. 如果动态程序集是持久性的和已写入磁盘之前GetType是调用,加载程序在磁盘上找到已保存的程序集,加载该程序集,并且该程序集中检索的类型。If the dynamic assembly is persistent and has been written to disk before GetType is called, the loader finds the saved assembly on disk, loads that assembly, and retrieves the type from that assembly. 如果该程序集未保存到磁盘GetType调用时,该方法将返回nullIf the assembly has not been saved to disk when GetType is called, the method returns null. GetType 不能理解瞬态动态程序集;因此,调用GetType来检索中暂时性的类型的动态程序集返回nullGetType does not understand transient dynamic assemblies; therefore, calling GetType to retrieve a type in a transient dynamic assembly returns null.

若要使用GetType上的动态模块订阅AppDomain.AssemblyResolve事件,并调用GetType然后再进行保存。To use GetType on a dynamic module, subscribe to the AppDomain.AssemblyResolve event and call GetType before saving. 否则,你会在内存中出现的程序集的两个副本。Otherwise, you will get two copies of the assembly in memory.

下表显示了返回通过基类的成员Get方法反射的类型上时。The following table shows what members of a base class are returned by the Get methods when reflecting on a type.

成员类型Member Type StaticStatic 非静态Non-Static
构造函数Constructor NoNo NoNo
字段Field NoNo 可以。Yes. 字段始终是按名称和签名隐藏。A field is always hide-by-name-and-signature.
事件Event 不适用Not applicable 通用类型系统规则是继承是实现的属性的方法相同。The common type system rule is that the inheritance is the same as that of the methods that implement the property. 反射将属性视为由名称和签名隐藏。Reflection treats properties as hide-by-name-and-signature. 请参阅备注 2 下面。See note 2 below.
方法Method NoNo 可以。Yes. 方法 (包括虚拟和非虚拟) 可以隐藏通过名称或按名称和签名隐藏。A method (both virtual and non-virtual) can be hide-by-name or hide-by-name-and-signature.
嵌套的类型Nested Type NoNo NoNo
PropertyProperty 不适用Not applicable 通用类型系统规则是继承是实现的属性的方法相同。The common type system rule is that the inheritance is the same as that of the methods that implement the property. 反射将属性视为由名称和签名隐藏。Reflection treats properties as hide-by-name-and-signature. 请参阅备注 2 下面。See note 2 below.
  1. 按名称和签名隐藏会考虑所有的签名,包括自定义修饰符,部分返回类型、 参数类型、 标记和非托管调用约定。Hide-by-name-and-signature considers all of the parts of the signature, including custom modifiers, return types, parameter types, sentinels, and unmanaged calling conventions. 这是二进制比较。This is a binary comparison.

  2. 为反射,属性和事件均由名称和签名隐藏。For reflection, properties and events are hide-by-name-and-signature. 如果具有 get 和 set 访问器的属性中的基类,但派生的类具有一个 get 访问器,派生的类属性隐藏基类属性,并且你将无法再访问基类上的资源库。If you have a property with both a get and a set accessor in the base class, but the derived class has only a get accessor, the derived class property hides the base class property, and you will not be able to access the setter on the base class.

  3. 自定义特性不是通用类型系统的一部分。Custom attributes are not part of the common type system.

数组或 COM 类型是不执行搜索,除非它们已加载到表中的可用类。Arrays or COM types are not searched for unless they have already been loaded into the table of available classes.

typeName 可将类型名称由限定其命名空间或程序集限定名,包括程序集名称规范。typeName can be the type name qualified by its namespace or an assembly-qualified name that includes an assembly name specification. 请参阅 AssemblyQualifiedNameSee AssemblyQualifiedName.

如果typeName包括命名空间而不是程序集名称,此方法搜索仅调用对象的程序集和 Mscorlib.dll,按该顺序。If typeName includes the namespace but not the assembly name, this method searches only the calling object's assembly and Mscorlib.dll, in that order. 如果使用的部分或完整的程序集名称完全限定类型名称,此方法搜索中指定的程序集。If typeName is fully qualified with the partial or complete assembly name, this method searches in the specified assembly. 如果该程序集具有强名称,则需要一个完整的程序集名称。If the assembly has a strong name, a complete assembly name is required.

AssemblyQualifiedName属性将返回包括嵌套的类型、 程序集名称和泛型类型参数的完全限定的类型名称。The AssemblyQualifiedName property returns a fully qualified type name including nested types, the assembly name, and generic type arguments. 支持公共语言运行时的所有编译器将都发出嵌套类的简单名称和反射构造查询时,根据以下约定的重整的名称。All compilers that support the common language runtime will emit the simple name of a nested class, and reflection constructs a mangled name when queried, in accordance with the following conventions.

备注

在.NET Framework 2.0 版中,处理器体系结构添加到程序集标识,并可以指定为程序集名称字符串的一部分。In the .NET Framework version 2.0, processor architecture is added to assembly identity, and can be specified as part of assembly name strings. 例如,"ProcessorArchitecture = msil"。For example, "ProcessorArchitecture=msil". 但是,不包括在返回的字符串AssemblyQualifiedName属性,出于兼容性原因。However, it is not included in the string returned by the AssemblyQualifiedName property, for compatibility reasons. 此外可以通过创建加载类型AssemblyName对象并将其传递到的相应重载Load方法。You can also load types by creating an AssemblyName object and passing it to an appropriate overload of the Load method. 然后,可以使用Assembly.GetType方法来加载类型的程序集中。You can then use the Assembly.GetType method to load types from the assembly. 另请参阅 AssemblyName.ProcessorArchitectureSee also AssemblyName.ProcessorArchitecture.

分隔符Delimiter 含义Meaning
反斜杠 (\)Backslash (\) 转义符。Escape character.
反撇号 (')Backtick (`) 位于一个或多个数字表示的类型参数,位于末尾的泛型类型的名称数。Precedes one or more digits representing the number of type parameters, located at the end of the name of a generic type.
方括号 ([])Brackets ([]) 泛型类型参数列表,用于构造泛型类型; 括起来在类型参数列表中,则将程序集限定的类型。Enclose a generic type argument list, for a constructed generic type; within a type argument list, enclose an assembly-qualified type.
逗号 (,)Comma (,) 程序集名称前面。Precedes the Assembly name.
句点 (.)Period (.) 表示命名空间标识符。Denotes namespace identifiers.
加号 (+)Plus sign (+) 位于嵌套的类。Precedes a nested class.

例如,一个类的完全限定的名称可能如下所示:For example, the fully qualified name for a class might look like this:

TopNamespace.SubNameSpace.ContainingClass+NestedClass,MyAssembly  

如果命名空间为 TopNamespace.Sub+Namespace,则必须在字符串前面加一个转义字符加号 (+) (\) 以防止它被解释为嵌套分隔符。If the namespace were TopNamespace.Sub+Namespace, then the string would have to precede the plus sign (+) with an escape character (\) to prevent it from being interpreted as a nesting separator. 反射发出此字符串,如下所示:Reflection emits this string as follows:

TopNamespace.Sub\+Namespace.ContainingClass+NestedClass,MyAssembly  

一个"+ +"变为"\+\+",和一个"\"变为"\\"。A "++" becomes "\+\+", and a "\" becomes "\\".

可以保持此限定的名,并在以后使用它来加载TypeThis qualified name can be persisted and later used to load the Type. 若要搜索并加载Type,使用GetType使用类型名称只或用程序集限定的类型名称。To search for and load a Type, use GetType either with the type name only or with the assembly qualified type name. GetType 使用类型名称只会查找Type中调用方的程序集,然后在系统程序集。GetType with the type name only will look for the Type in the caller's assembly and then in the System assembly. GetType 使用程序集限定的类型名称将寻找Type在任何程序集中。GetType with the assembly qualified type name will look for the Type in any assembly.

类型名称可能包含尾随字符表示的类型,如类型是引用类型、 指针类型或数组类型有关的其他信息。Type names may include trailing characters that denote additional information about the type, such as whether the type is a reference type, a pointer type or an array type. 若要检索的类型名称而无需这些尾随字符,请使用t.GetElementType().ToString(),其中t的类型。To retrieve the type name without these trailing characters, use t.GetElementType().ToString(), where t is the type.

空格都是相关程序集名称之外的所有类型的名称组件中。Spaces are relevant in all type name components except the assembly name. 在程序集名称,分隔符前的空格相关,但是、 分隔符之后的空格将被忽略。In the assembly name, spaces before the ',' separator are relevant, but spaces after the ',' separator are ignored.

泛型类型的名称结尾反引号 (`) 跟数字表示的泛型类型参数的数目。The name of a generic type ends with a backtick (`) followed by digits representing the number of generic type arguments. 此名称重整的目的是允许编译器来支持在同一作用域中发生的泛型类型具有相同名称但具有不同数量的类型参数。The purpose of this name mangling is to allow compilers to support generic types with the same name but with different numbers of type parameters, occurring in the same scope. 例如,反射返回重整的名称Tuple1and元组2从泛型方法Tuple(Of T)Tuple(Of T0, T1)在 Visual Basic 中,或Tuple<T>和元组\<T0, T1>Visual C# 中。For example, reflection returns the mangled names Tuple1andTuple2 from the generic methods Tuple(Of T) and Tuple(Of T0, T1) in Visual Basic, or Tuple<T> and Tuple\<T0, T1> in Visual C#.

对于泛型类型,类型实参列表括在方括号内,并由逗号分隔的类型参数。For generic types, the type argument list is enclosed in brackets, and the type arguments are separated by commas. 例如,泛型Dictionary<TKey,TValue>具有两个类型参数。For example, a generic Dictionary<TKey,TValue> has two type parameters. 一个Dictionary<TKey,TValue>MyType类型的密钥与String可能表示,如下所示:A Dictionary<TKey,TValue> of MyType with keys of type String might be represented as follows:

System.Collections.Generic.Dictionary`2[System.String,MyType]  

若要指定类型实参列表内的某个程序集限定类型,请将放在括号内的程序集限定类型。To specify an assembly-qualified type within a type argument list, enclose the assembly-qualified type within brackets. 否则,单独的程序集限定名称的逗号解释为限制其他类型参数。Otherwise, the commas that separate the parts of the assembly-qualified name are interpreted as delimiting additional type arguments. 例如,Dictionary<TKey,TValue>MyTypefromMyAssembly.dll,与键的类型String,可能会按如下所示指定:For example, a Dictionary<TKey,TValue> of MyType fromMyAssembly.dll, with keys of type String, might be specified as follows:

Type.GetType("System.Collections.Generic.Dictionary`2[System.String,[MyType,MyAssembly]]")  

备注

程序集限定的类型可以括在括号中,仅当它出现在类型参数列表。An assembly-qualified type can be enclosed in brackets only when it appears within a type parameter list. 在类型参数列表中搜索程序集限定和未限定类型的规则是限定和未限定的非泛型类型的规则相同。The rules for searching assemblies for qualified and unqualified types in type parameter lists are the same as the rules for qualified and unqualified nongeneric types.

可以为 null 的类型是泛型类型的一种特殊情况。Nullable types are a special case of generic types. 例如,一个可以为 nullInt32由字符串"System.Nullable'1[System.Int32]"表示。For example, a nullable Int32 is represented by the string "System.Nullable`1[System.Int32]".

备注

在C#, C++,和 Visual Basic 还可以获取可为 null 类型使用类型运算符。In C#, C++, and Visual Basic you can also get nullable types using type operators. 例如,可以为 nullBoolean返回类型typeof(Nullable<bool>)中C#,也可由Nullable<Boolean>::typeid在C++,并通过GetType(Nullable(Of Boolean))Visual Basic 中。For example, the nullable Boolean type is returned by typeof(Nullable<bool>) in C#, by Nullable<Boolean>::typeid in C++, and by GetType(Nullable(Of Boolean)) in Visual Basic.

下表显示了与使用的语法GetType为各种类型。The following table shows the syntax you use with GetType for various types.

若要获取To Get 使用Use
一个可以为 null Int32A nullable Int32 Type.GetType("System.Nullable`1[System.Int32]")
指向非托管的指针 MyTypeAn unmanaged pointer to MyType Type.GetType("MyType*")
非托管的指针到指向 MyTypeAn unmanaged pointer to a pointer to MyType Type.GetType("MyType**")
托管的指针或引用 MyTypeA managed pointer or reference to MyType Type.GetType("MyType&")Type.GetType("MyType&"). 请注意,与指针不同,引用仅限于一个级别。Note that unlike pointers, references are limited to one level.
父类和嵌套的类A parent class and a nested class Type.GetType("MyParentClass+MyNestedClass")
一维数组下限为 0A one-dimensional array with a lower bound of 0 Type.GetType("MyType[]")
一维数组下限未知A one-dimensional array with an unknown lower bound Type.GetType("MyType[*]")
N 维数组An n-dimensional array 在中总共 n-1 次括号逗号 (,)。A comma (,) inside the brackets a total of n-1 times. 例如,System.Object[,,]表示一个三维Object数组。For example, System.Object[,,] represents a three-dimensional Object array.
数组的一维数组An array of one-dimensional arrays Type.GetType("MyType[][]")
矩形二维数组具有未知的下限A rectangular two-dimensional array with unknown lower bounds Type.GetType("MyType[,]")
具有一个类型参数的泛型类型A generic type with one type argument Type.GetType("MyGenericType`1[MyType]")
具有两个类型参数的泛型类型A generic type with two type arguments Type.GetType("MyGenericType`2[MyType,AnotherType]")
具有两个程序集限定类型参数的泛型类型A generic type with two assembly-qualified type arguments Type.GetType("MyGenericType`2[[MyType,MyAssembly],[AnotherType,AnotherAssembly]]")
具有程序集限定类型参数的程序集限定的泛型类型An assembly-qualified generic type with an assembly-qualified type argument Type.GetType("MyGenericType`1[[MyType,MyAssembly]],MyGenericTypeAssembly")
一个其类型参数为具有两个类型参数的泛型类型的泛型类型A generic type whose type argument is a generic type with two type arguments Type.GetType("MyGenericType`1[AnotherGenericType`2[MyType,AnotherType]]")
另请参阅

GetType(String, Boolean) GetType(String, Boolean) GetType(String, Boolean) GetType(String, Boolean)

获取具有指定名称的 Type,指定是否执行区分大小写的搜索,以及在找不到类型时是否引发异常。Gets the Type with the specified name, performing a case-sensitive search and specifying whether to throw an exception if the type is not found.

public:
 static Type ^ GetType(System::String ^ typeName, bool throwOnError);
public static Type GetType (string typeName, bool throwOnError);
static member GetType : string * bool -> Type
Public Shared Function GetType (typeName As String, throwOnError As Boolean) As Type

参数

typeName
String String String String

要获取的类型的程序集限定名称。The assembly-qualified name of the type to get. 请参阅 AssemblyQualifiedNameSee AssemblyQualifiedName. 如果该类型位于当前正在执行的程序集中或者 Mscorlib.dll 中,则提供由命名空间限定的类型名称就足够了。If the type is in the currently executing assembly or in Mscorlib.dll, it is sufficient to supply the type name qualified by its namespace.

throwOnError
Boolean Boolean Boolean Boolean

如果为 true,则在找不到该类型时引发异常;如果为 false,则返回 nulltrue to throw an exception if the type cannot be found; false to return null. 指定 false 还会取消某些其他异常条件,但并不取消所有条件。Specifying false also suppresses some other exception conditions, but not all of them. 请参见“异常”部分。See the Exceptions section.

返回

具有指定名称的类型。The type with the specified name. 如果找不到该类型,则 throwOnError 参数指定是返回 null 还是引发异常。If the type is not found, the throwOnError parameter specifies whether null is returned or an exception is thrown. 在某些情况下,将引发异常,而不考虑 throwOnError 的值。In some cases, an exception is thrown regardless of the value of throwOnError. 请参见“异常”部分。See the Exceptions section.

异常

调用了类初始值设定项,且该项引发了异常。A class initializer is invoked and throws an exception.

throwOnErrortrue,且找不到该类型。throwOnError is true and the type is not found.

- 或 --or- throwOnError 的值为 true,且 typeName 包含无效字符,例如嵌入的制表符。throwOnError is true and typeName contains invalid characters, such as an embedded tab. - 或 --or- throwOnError 的值为 true,且 typeName 为空字符串。throwOnError is true and typeName is an empty string.

- 或 --or- throwOnError 的值为 true,且 typeName 表示大小无效的数组类型。throwOnError is true and typeName represents an array type with an invalid size.

- 或 --or- typeName 表示 TypedReference 的数组。typeName represents an array of TypedReference.

throwOnErrortrue,且 typeName 包含无效的语法。throwOnError is true and typeName contains invalid syntax. 例如:"MyType[,*,]"。For example, "MyType[,*,]".

- 或 --or- typeName 表示将指针类型、ByRef 类型或 Void 作为其类型参数之一的泛型类型。typeName represents a generic type that has a pointer type, a ByRef type, or Void as one of its type arguments.

- 或 --or- typeName 表示具有错误的类型参数数目的泛型类型。typeName represents a generic type that has an incorrect number of type arguments.

- 或 --or- typeName 表示泛型类型,并且其类型参数之一不满足相应类型参数的约束。typeName represents a generic type, and one of its type arguments does not satisfy the constraints for the corresponding type parameter.

throwOnError 的值为 true,且未找到程序集或程序集的某个依赖项。throwOnError is true and the assembly or one of its dependencies was not found.

适用于 Windows 应用商店应用的 .NET可移植类库中,改为捕获基类异常 IOExceptionIn the .NET for Windows Store apps or the Portable Class Library, catch the base class exception, IOException, instead.

找到了程序集或其依赖项之一,但无法加载它们。The assembly or one of its dependencies was found, but could not be loaded.

程序集或其依赖项之一无效。The assembly or one of its dependencies is not valid.

- 或 --or- 当前加载的是公共语言运行时 2.0 版或更高版本,而程序集使用更高的版本编译。Version 2.0 or later of the common language runtime is currently loaded, and the assembly was compiled with a later version.

示例

下面的示例检索的类型System.Int32,并使用该类型对象以显示FullName属性的System.Int32The following example retrieves the type of System.Int32 and uses that type object to display the FullName property of System.Int32. 如果不存在的程序集引用类型对象,此示例将引发异常。If a type object refers to an assembly that does not exist, this example throws an exception.

using namespace System;

int main()
{
   try {
      // Get the type of a specified class.
      Type^ myType1 = Type::GetType( "System.Int32" );
      Console::WriteLine( "The full name is {0}.\n", myType1->FullName );
   }
   catch ( TypeLoadException^ e ) {
       Console::WriteLine("{0}: Unable to load type System.Int32",
                          e->GetType()->Name);
   }

   try {
      // Since NoneSuch does not exist in this assembly, GetType throws a TypeLoadException.
      Type^ myType2 = Type::GetType( "NoneSuch", true );
      Console::WriteLine( "The full name is {0}.", myType2->FullName );
   }
   catch ( TypeLoadException^ e ) {
       Console::WriteLine("{0}: Unable to load type NoneSuch",
                          e->GetType()->Name);
   }

}
// The example displays the following output:
//       The full name is System.Int32.
//
//       TypeLoadException: Unable to load type NoneSuch
using System;

class Example
{
     public static void Main()
     {
         try {
             // Get the type of a specified class.
             Type myType1 = Type.GetType("System.Int32");
             Console.WriteLine("The full name is {0}.\n", myType1.FullName);
         }
         catch (TypeLoadException e)
         {
            Console.WriteLine("{0}: Unable to load type System.Int32", e.GetType().Name);
         }

         try {
             // Since NoneSuch does not exist in this assembly, GetType throws a TypeLoadException.
             Type myType2 = Type.GetType("NoneSuch", true);
             Console.WriteLine("The full name is {0}.", myType2.FullName);
         }
         catch(TypeLoadException e) {
            Console.WriteLine("{0}: Unable to load type NoneSuch", e.GetType().Name);
         }
     }
}
// The example displays the following output:
//       The full name is System.Int32.
//
//       TypeLoadException: Unable to load type NoneSuch
Class Example
     Public Shared Sub Main()
         Try
             ' Get the type of the specified class.
             Dim myType1 As Type = Type.GetType("System.Int32")
             Console.WriteLine("The full name is {0}.", myType1.FullName)
         Catch e As TypeLoadException
             Console.WriteLine("{0}: Unable to load type System.Int32",
                               e.GetType().Name)
         End Try

         Console.WriteLine()

         Try
             ' Since NoneSuch does not exist in this assembly, GetType throws a TypeLoadException.
             Dim myType2 As Type = Type.GetType("NoneSuch", True)
             Console.WriteLine("The full name is {0}.", myType2.FullName)
         Catch e As TypeLoadException
             Console.WriteLine("{0}: Unable to load type NoneSuch", e.GetType().Name)
         End Try
     End Sub
End Class
' The example displays the following output:
'       The full name is System.Int32.
'
'       TypeLoadException: Unable to load type NoneSuch

注解

可以使用GetType方法来获取Type如果你知道可以从获取其程序集限定名称的另一个程序集中的类型的对象AssemblyQualifiedNameYou can use the GetType method to obtain a Type object for a type in another assembly if you know its assembly-qualified name, which can be obtained from AssemblyQualifiedName. GetType 导致在指定的程序集加载typeNameGetType causes loading of the assembly specified in typeName. 您也可以加载使用程序集Assembly.Load方法,并使用Assembly.GetTypeAssembly.GetTypes方法以获取Type对象。You can also load an assembly using the Assembly.Load method, and then use the Assembly.GetType or Assembly.GetTypes method to get Type objects. 如果类型是在编译时已知到您的程序的程序集中,则使用更加高效typeof在C#或GetType在 Visual Basic 中的运算符。If a type is in an assembly known to your program at compile time, it is more efficient to use typeof in C# or the GetType operator in Visual Basic.

GetType 仅适用于从磁盘加载的程序集。GetType only works on assemblies loaded from disk. 如果您调用GetType来查找使用定义动态程序集中定义的类型System.Reflection.Emit服务,你可能会收到不一致的行为。If you call GetType to look up a type defined in a dynamic assembly defined using the System.Reflection.Emit services, you might get inconsistent behavior. 行为取决于动态程序集是持久的也就是说,创建使用RunAndSaveSave访问模式的System.Reflection.Emit.AssemblyBuilderAccess枚举。The behavior depends on whether the dynamic assembly is persistent, that is, created using the RunAndSave or Save access modes of the System.Reflection.Emit.AssemblyBuilderAccess enumeration. 如果动态程序集是持久性的和已写入磁盘之前GetType是调用,加载程序在磁盘上找到已保存的程序集,加载该程序集,并且该程序集中检索的类型。If the dynamic assembly is persistent and has been written to disk before GetType is called, the loader finds the saved assembly on disk, loads that assembly, and retrieves the type from that assembly. 如果该程序集未保存到磁盘GetType调用时,该方法将返回nullIf the assembly has not been saved to disk when GetType is called, the method returns null. GetType 不能理解瞬态动态程序集;因此,调用GetType来检索中暂时性的类型的动态程序集返回nullGetType does not understand transient dynamic assemblies; therefore, calling GetType to retrieve a type in a transient dynamic assembly returns null.

若要使用GetType上的动态模块订阅AppDomain.AssemblyResolve事件,并调用GetType然后再进行保存。To use GetType on a dynamic module, subscribe to the AppDomain.AssemblyResolve event and call GetType before saving. 否则,你会在内存中出现的程序集的两个副本。Otherwise, you will get two copies of the assembly in memory.

throwOnError参数指定并发生什么类型未找到,也抑制了某些其他异常条件,如异常部分中所述。The throwOnError parameter specifies what happens when the type is not found, and also suppresses certain other exception conditions, as described in the Exceptions section. 而不考虑值引发一些异常throwOnErrorSome exceptions are thrown regardless of the value of throwOnError. 例如,如果找到但不能加载,类型TypeLoadException引发即使throwOnErrorfalseFor example, if the type is found but cannot be loaded, a TypeLoadException is thrown even if throwOnError is false.

下表显示了返回通过基类的成员Get方法反射的类型上时。The following table shows what members of a base class are returned by the Get methods when reflecting on a type.

成员类型Member Type StaticStatic 非静态Non-Static
构造函数Constructor NoNo NoNo
字段Field NoNo 可以。Yes. 字段始终是按名称和签名隐藏。A field is always hide-by-name-and-signature.
事件Event 不适用Not applicable 通用类型系统规则是继承是实现的属性的方法相同。The common type system rule is that the inheritance is the same as that of the methods that implement the property. 反射将属性视为由名称和签名隐藏。Reflection treats properties as hide-by-name-and-signature. 请参阅备注 2 下面。See note 2 below.
方法Method NoNo 可以。Yes. 方法 (包括虚拟和非虚拟) 可以隐藏通过名称或按名称和签名隐藏。A method (both virtual and non-virtual) can be hide-by-name or hide-by-name-and-signature.
嵌套的类型Nested Type NoNo NoNo
PropertyProperty 不适用Not applicable 通用类型系统规则是继承是实现的属性的方法相同。The common type system rule is that the inheritance is the same as that of the methods that implement the property. 反射将属性视为由名称和签名隐藏。Reflection treats properties as hide-by-name-and-signature. 请参阅备注 2 下面。See note 2 below.
  1. 按名称和签名隐藏会考虑所有的签名,包括自定义修饰符,部分返回类型、 参数类型、 标记和非托管调用约定。Hide-by-name-and-signature considers all of the parts of the signature, including custom modifiers, return types, parameter types, sentinels, and unmanaged calling conventions. 这是二进制比较。This is a binary comparison.

  2. 为反射,属性和事件均由名称和签名隐藏。For reflection, properties and events are hide-by-name-and-signature. 如果具有 get 和 set 访问器的属性中的基类,但派生的类具有一个 get 访问器,派生的类属性隐藏基类属性,并且你将无法再访问基类上的资源库。If you have a property with both a get and a set accessor in the base class, but the derived class has only a get accessor, the derived class property hides the base class property, and you will not be able to access the setter on the base class.

  3. 自定义特性不是通用类型系统的一部分。Custom attributes are not part of the common type system.

数组或 COM 类型是不执行搜索,除非它们已加载到表中的可用类。Arrays or COM types are not searched for unless they have already been loaded into the table of available classes.

typeName 可将类型名称由限定其命名空间或程序集限定名,包括程序集名称规范。typeName can be the type name qualified by its namespace or an assembly-qualified name that includes an assembly name specification. 请参阅 AssemblyQualifiedNameSee AssemblyQualifiedName.

如果typeName包括命名空间而不是程序集名称,此方法搜索仅调用对象的程序集和 Mscorlib.dll,按该顺序。If typeName includes the namespace but not the assembly name, this method searches only the calling object's assembly and Mscorlib.dll, in that order. 如果使用的部分或完整的程序集名称完全限定类型名称,此方法搜索中指定的程序集。If typeName is fully qualified with the partial or complete assembly name, this method searches in the specified assembly. 如果该程序集具有强名称,则需要一个完整的程序集名称。If the assembly has a strong name, a complete assembly name is required.

AssemblyQualifiedName属性将返回包括嵌套的类型、 程序集名称和泛型参数的完全限定的类型名称。The AssemblyQualifiedName property returns a fully qualified type name including nested types, the assembly name, and generic arguments. 支持公共语言运行时的所有编译器将都发出嵌套类的简单名称和反射构造查询时,根据以下约定的重整的名称。All compilers that support the common language runtime will emit the simple name of a nested class, and reflection constructs a mangled name when queried, in accordance with the following conventions.

备注

在.NET Framework 2.0 版中,处理器体系结构添加到程序集标识,并可以指定为程序集名称字符串的一部分。In the .NET Framework version 2.0, processor architecture is added to assembly identity, and can be specified as part of assembly name strings. 例如,"ProcessorArchitecture = msil"。For example, "ProcessorArchitecture=msil". 但是,不包括在返回的字符串AssemblyQualifiedName属性,出于兼容性原因。However, it is not included in the string returned by the AssemblyQualifiedName property, for compatibility reasons. 此外可以通过创建加载类型AssemblyName对象并将其传递到的相应重载Load方法。You can also load types by creating an AssemblyName object and passing it to an appropriate overload of the Load method. 然后,可以使用Assembly.GetType方法来加载类型的程序集中。You can then use the Assembly.GetType method to load types from the assembly. 另请参阅 AssemblyName.ProcessorArchitectureSee also AssemblyName.ProcessorArchitecture.

分隔符Delimiter 含义Meaning
反斜杠 (\)Backslash (\) 转义符。Escape character.
反撇号 (')Backtick (`) 位于一个或多个数字表示的类型参数,位于末尾的泛型类型的名称数。Precedes one or more digits representing the number of type parameters, located at the end of the name of a generic type.
方括号 ([])Brackets ([]) 泛型类型参数列表,用于构造泛型类型; 括起来在类型参数列表中,则将程序集限定的类型。Enclose a generic type argument list, for a constructed generic type; within a type argument list, enclose an assembly-qualified type.
逗号 (,)Comma (,) 程序集名称前面。Precedes the Assembly name.
句点 (.)Period (.) 表示命名空间标识符。Denotes namespace identifiers.
加号 (+)Plus sign (+) 位于嵌套的类。Precedes a nested class.

例如,一个类的完全限定的名称可能如下所示:For example, the fully qualified name for a class might look like this:

TopNamespace.SubNameSpace.ContainingClass+NestedClass,MyAssembly  

如果命名空间为 TopNamespace.Sub+Namespace,则必须在字符串前面加一个转义字符加号 (+) (\) 以防止它被解释为嵌套分隔符。If the namespace were TopNamespace.Sub+Namespace, then the string would have to precede the plus sign (+) with an escape character (\) to prevent it from being interpreted as a nesting separator. 反射发出此字符串,如下所示:Reflection emits this string as follows:

TopNamespace.Sub\+Namespace.ContainingClass+NestedClass,MyAssembly  

一个"+ +"变为"\+\+",和一个"\"变为"\\"。A "++" becomes "\+\+", and a "\" becomes "\\".

可以保持此限定的名,并在以后使用它来加载TypeThis qualified name can be persisted and later used to load the Type. 若要搜索并加载Type,使用GetType使用类型名称只或用程序集限定的类型名称。To search for and load a Type, use GetType either with the type name only or with the assembly qualified type name. GetType 使用类型名称只会查找Type中调用方的程序集,然后在系统程序集。GetType with the type name only will look for the Type in the caller's assembly and then in the System assembly. GetType 使用程序集限定的类型名称将寻找Type在任何程序集中。GetType with the assembly qualified type name will look for the Type in any assembly.

类型名称可能包含尾随字符表示的类型,如类型是引用类型、 指针类型或数组类型有关的其他信息。Type names may include trailing characters that denote additional information about the type, such as whether the type is a reference type, a pointer type or an array type. 若要检索的类型名称而无需这些尾随字符,请使用t.GetElementType().ToString(),其中t的类型。To retrieve the type name without these trailing characters, use t.GetElementType().ToString(), where t is the type.

空格都是相关程序集名称之外的所有类型的名称组件中。Spaces are relevant in all type name components except the assembly name. 在程序集名称,分隔符前的空格相关,但是、 分隔符之后的空格将被忽略。In the assembly name, spaces before the ',' separator are relevant, but spaces after the ',' separator are ignored.

泛型类型的名称结尾反引号 (`) 跟数字表示的泛型类型参数的数目。The name of a generic type ends with a backtick (`) followed by digits representing the number of generic type arguments. 此名称重整的目的是允许编译器来支持在同一作用域中发生的泛型类型具有相同名称但具有不同数量的类型参数。The purpose of this name mangling is to allow compilers to support generic types with the same name but with different numbers of type parameters, occurring in the same scope. 例如,反射返回重整的名称Tuple1and元组2从泛型方法Tuple(Of T)Tuple(Of T0, T1)在 Visual Basic 中,或Tuple<T>和元组\<T0, T1>Visual C# 中。For example, reflection returns the mangled names Tuple1andTuple2 from the generic methods Tuple(Of T) and Tuple(Of T0, T1) in Visual Basic, or Tuple<T> and Tuple\<T0, T1> in Visual C#.

对于泛型类型,类型实参列表括在方括号内,并由逗号分隔的类型参数。For generic types, the type argument list is enclosed in brackets, and the type arguments are separated by commas. 例如,泛型Dictionary<TKey,TValue>具有两个类型参数。For example, a generic Dictionary<TKey,TValue> has two type parameters. 一个Dictionary<TKey,TValue>MyType类型的密钥与String可能表示,如下所示:A Dictionary<TKey,TValue> of MyType with keys of type String might be represented as follows:

System.Collections.Generic.Dictionary`2[System.String,MyType]  

若要指定类型实参列表内的某个程序集限定类型,请将放在括号内的程序集限定类型。To specify an assembly-qualified type within a type argument list, enclose the assembly-qualified type within brackets. 否则,单独的程序集限定名称的逗号解释为限制其他类型参数。Otherwise, the commas that separate the parts of the assembly-qualified name are interpreted as delimiting additional type arguments. 例如,Dictionary<TKey,TValue>MyTypeMyAssembly.dll,与键的类型从String,可能会按如下所示指定:For example, a Dictionary<TKey,TValue> of MyType from MyAssembly.dll, with keys of type String, might be specified as follows:

Type.GetType("System.Collections.Generic.Dictionary`2[System.String,[MyType,MyAssembly]]")  

备注

程序集限定的类型可以括在括号中,仅当它出现在类型参数列表。An assembly-qualified type can be enclosed in brackets only when it appears within a type parameter list. 在类型参数列表中搜索程序集限定和未限定类型的规则是限定和未限定的非泛型类型的规则相同。The rules for searching assemblies for qualified and unqualified types in type parameter lists are the same as the rules for qualified and unqualified nongeneric types.

可以为 null 的类型是泛型类型的一种特殊情况。Nullable types are a special case of generic types. 例如,一个可以为 nullInt32由字符串"System.Nullable'1[System.Int32]"表示。For example, a nullable Int32 is represented by the string "System.Nullable`1[System.Int32]".

备注

在C#, C++,和 Visual Basic 还可以获取可为 null 类型使用类型运算符。In C#, C++, and Visual Basic you can also get nullable types using type operators. 例如,可以为 nullBoolean返回类型typeof(Nullable<bool>)中C#,也可由Nullable<Boolean>::typeid在C++,并通过GetType(Nullable(Of Boolean))Visual Basic 中。For example, the nullable Boolean type is returned by typeof(Nullable<bool>) in C#, by Nullable<Boolean>::typeid in C++, and by GetType(Nullable(Of Boolean)) in Visual Basic.

下表显示了与使用的语法GetType为各种类型。The following table shows the syntax you use with GetType for various types.

若要获取To Get 使用Use
一个可以为 null Int32A nullable Int32 Type.GetType("System.Nullable`1[System.Int32]")
指向非托管的指针 MyTypeAn unmanaged pointer to MyType Type.GetType("MyType*")
非托管的指针到指向 MyTypeAn unmanaged pointer to a pointer to MyType Type.GetType("MyType**")
托管的指针或引用 MyTypeA managed pointer or reference to MyType Type.GetType("MyType&")Type.GetType("MyType&"). 请注意,与指针不同,引用仅限于一个级别。Note that unlike pointers, references are limited to one level.
父类和嵌套的类A parent class and a nested class Type.GetType("MyParentClass+MyNestedClass")
一维数组下限为 0A one-dimensional array with a lower bound of 0 Type.GetType("MyArray[]")
一维数组下限未知A one-dimensional array with an unknown lower bound Type.GetType("MyArray[*]")
N 维数组An n-dimensional array 在中总共 n-1 次括号逗号 (,)。A comma (,) inside the brackets a total of n-1 times. 例如,System.Object[,,]表示一个三维Object数组。For example, System.Object[,,] represents a three-dimensional Object array.
二维数组的数组A two-dimensional array's array Type.GetType("MyArray[][]")
矩形二维数组具有未知的下限A rectangular two-dimensional array with unknown lower bounds Type.GetType("MyArray[,]")
具有一个类型参数的泛型类型A generic type with one type argument Type.GetType("MyGenericType`1[MyType]")
具有两个类型参数的泛型类型A generic type with two type arguments Type.GetType("MyGenericType`2[MyType,AnotherType]")
具有两个程序集限定类型参数的泛型类型A generic type with two assembly-qualified type arguments Type.GetType("MyGenericType`2[[MyType,MyAssembly],[AnotherType,AnotherAssembly]]")
具有程序集限定类型参数的程序集限定的泛型类型An assembly-qualified generic type with an assembly-qualified type argument Type.GetType("MyGenericType`1[[MyType,MyAssembly]],MyGenericTypeAssembly")
一个其类型参数为具有两个类型参数的泛型类型的泛型类型A generic type whose type argument is a generic type with two type arguments Type.GetType("MyGenericType`1[AnotherGenericType`2[MyType,AnotherType]]")
另请参阅

GetType(String, Boolean, Boolean) GetType(String, Boolean, Boolean) GetType(String, Boolean, Boolean) GetType(String, Boolean, Boolean)

获取具有指定名称的 Type,指定是否执行区分大小写的搜索,以及在找不到类型时是否引发异常。Gets the Type with the specified name, specifying whether to throw an exception if the type is not found and whether to perform a case-sensitive search.

public:
 static Type ^ GetType(System::String ^ typeName, bool throwOnError, bool ignoreCase);
public static Type GetType (string typeName, bool throwOnError, bool ignoreCase);
static member GetType : string * bool * bool -> Type
Public Shared Function GetType (typeName As String, throwOnError As Boolean, ignoreCase As Boolean) As Type

参数

typeName
String String String String

要获取的类型的程序集限定名称。The assembly-qualified name of the type to get. 请参阅 AssemblyQualifiedNameSee AssemblyQualifiedName. 如果该类型位于当前正在执行的程序集中或者 Mscorlib.dll 中,则提供由命名空间限定的类型名称就足够了。If the type is in the currently executing assembly or in Mscorlib.dll, it is sufficient to supply the type name qualified by its namespace.

throwOnError
Boolean Boolean Boolean Boolean

如果为 true,则在找不到该类型时引发异常;如果为 false,则返回 nulltrue to throw an exception if the type cannot be found; false to return null. 指定 false 还会取消某些其他异常条件,但并不取消所有条件。Specifying false also suppresses some other exception conditions, but not all of them. 请参见“异常”部分。See the Exceptions section.

ignoreCase
Boolean Boolean Boolean Boolean

true 执行的搜索不区分大小写,则为 typeName;对 false 执行的搜索区分大小写,则为 typeNametrue to perform a case-insensitive search for typeName, false to perform a case-sensitive search for typeName.

返回

具有指定名称的类型。The type with the specified name. 如果找不到该类型,则 throwOnError 参数指定是返回 null 还是引发异常。If the type is not found, the throwOnError parameter specifies whether null is returned or an exception is thrown. 在某些情况下,将引发异常,而不考虑 throwOnError 的值。In some cases, an exception is thrown regardless of the value of throwOnError. 请参见“异常”部分。See the Exceptions section.

异常

调用了类初始值设定项,且该项引发了异常。A class initializer is invoked and throws an exception.

throwOnErrortrue,且找不到该类型。throwOnError is true and the type is not found.

- 或 --or- throwOnError 的值为 true,且 typeName 包含无效字符,例如嵌入的制表符。throwOnError is true and typeName contains invalid characters, such as an embedded tab. - 或 --or- throwOnError 的值为 true,且 typeName 为空字符串。throwOnError is true and typeName is an empty string.

- 或 --or- throwOnError 的值为 true,且 typeName 表示大小无效的数组类型。throwOnError is true and typeName represents an array type with an invalid size.

- 或 --or- typeName 表示 TypedReference 的数组。typeName represents an array of TypedReference.

throwOnErrortrue,且 typeName 包含无效的语法。throwOnError is true and typeName contains invalid syntax. 例如:"MyType[,*,]"。For example, "MyType[,*,]".

- 或 --or- typeName 表示将指针类型、ByRef 类型或 Void 作为其类型参数之一的泛型类型。typeName represents a generic type that has a pointer type, a ByRef type, or Void as one of its type arguments.

- 或 --or- typeName 表示具有错误的类型参数数目的泛型类型。typeName represents a generic type that has an incorrect number of type arguments.

- 或 --or- typeName 表示泛型类型,并且其类型参数之一不满足相应类型参数的约束。typeName represents a generic type, and one of its type arguments does not satisfy the constraints for the corresponding type parameter.

throwOnError 的值为 true,且未找到程序集或程序集的某个依赖项。throwOnError is true and the assembly or one of its dependencies was not found.

找到了程序集或其依赖项之一,但无法加载它们。The assembly or one of its dependencies was found, but could not be loaded.

程序集或其依赖项之一无效。The assembly or one of its dependencies is not valid.

- 或 --or- 当前加载的是公共语言运行时 2.0 版或更高版本,而程序集使用更高的版本编译。Version 2.0 or later of the common language runtime is currently loaded, and the assembly was compiled with a later version.

注解

可以使用GetType方法来获取Type如果你知道可以从获取其程序集限定名称的另一个程序集中的类型的对象AssemblyQualifiedNameYou can use the GetType method to obtain a Type object for a type in another assembly if you know its assembly-qualified name, which can be obtained from AssemblyQualifiedName. GetType 导致在指定的程序集加载typeNameGetType causes loading of the assembly specified in typeName. 您也可以加载使用程序集Assembly.Load方法,并使用Assembly.GetTypeAssembly.GetTypes方法以获取Type对象。You can also load an assembly using the Assembly.Load method, and then use the Assembly.GetType or Assembly.GetTypes method to get Type objects. 如果类型是在编译时已知到您的程序的程序集中,则使用更加高效typeof在C#或GetType在 Visual Basic 中的运算符。If a type is in an assembly known to your program at compile time, it is more efficient to use typeof in C# or the GetType operator in Visual Basic.

GetType 仅适用于从磁盘加载的程序集。GetType only works on assemblies loaded from disk. 如果您调用GetType来查找使用定义动态程序集中定义的类型System.Reflection.Emit服务,你可能会收到不一致的行为。If you call GetType to look up a type defined in a dynamic assembly defined using the System.Reflection.Emit services, you might get inconsistent behavior. 行为取决于动态程序集是持久的也就是说,创建使用RunAndSaveSave访问模式的System.Reflection.Emit.AssemblyBuilderAccess枚举。The behavior depends on whether the dynamic assembly is persistent, that is, created using the RunAndSave or Save access modes of the System.Reflection.Emit.AssemblyBuilderAccess enumeration. 如果动态程序集是持久性的和已写入磁盘之前GetType是调用,加载程序在磁盘上找到已保存的程序集,加载该程序集,并且该程序集中检索的类型。If the dynamic assembly is persistent and has been written to disk before GetType is called, the loader finds the saved assembly on disk, loads that assembly, and retrieves the type from that assembly. 如果该程序集未保存到磁盘GetType调用时,该方法将返回nullIf the assembly has not been saved to disk when GetType is called, the method returns null. GetType 不能理解瞬态动态程序集;因此,调用GetType来检索中暂时性的类型的动态程序集返回nullGetType does not understand transient dynamic assemblies; therefore, calling GetType to retrieve a type in a transient dynamic assembly returns null.

若要使用GetType上的动态模块订阅AppDomain.AssemblyResolve事件,并调用GetType然后再进行保存。To use GetType on a dynamic module, subscribe to the AppDomain.AssemblyResolve event and call GetType before saving. 否则,你会在内存中出现的程序集的两个副本。Otherwise, you will get two copies of the assembly in memory.

throwOnError参数指定并发生什么类型未找到,也抑制了某些其他异常条件,如异常部分中所述。The throwOnError parameter specifies what happens when the type is not found, and also suppresses certain other exception conditions, as described in the Exceptions section. 而不考虑值引发一些异常throwOnErrorSome exceptions are thrown regardless of the value of throwOnError. 例如,如果找到但不能加载,类型TypeLoadException引发即使throwOnErrorfalseFor example, if the type is found but cannot be loaded, a TypeLoadException is thrown even if throwOnError is false.

下表显示了返回通过基类的成员Get方法反射的类型上时。The following table shows what members of a base class are returned by the Get methods when reflecting on a type.

成员类型Member Type StaticStatic 非静态Non-Static
构造函数Constructor NoNo NoNo
字段Field NoNo 可以。Yes. 字段始终是按名称和签名隐藏。A field is always hide-by-name-and-signature.
事件Event 不适用Not applicable 通用类型系统规则是继承是实现的属性的方法相同。The common type system rule is that the inheritance is the same as that of the methods that implement the property. 反射将属性视为由名称和签名隐藏。Reflection treats properties as hide-by-name-and-signature. 请参阅备注 2 下面。See note 2 below.
方法Method NoNo 可以。Yes. 方法 (包括虚拟和非虚拟) 可以隐藏通过名称或按名称和签名隐藏。A method (both virtual and non-virtual) can be hide-by-name or hide-by-name-and-signature.
嵌套的类型Nested Type NoNo NoNo
PropertyProperty 不适用Not applicable 通用类型系统规则是继承是实现的属性的方法相同。The common type system rule is that the inheritance is the same as that of the methods that implement the property. 反射将属性视为由名称和签名隐藏。Reflection treats properties as hide-by-name-and-signature. 请参阅备注 2 下面。See note 2 below.
  1. 按名称和签名隐藏会考虑所有的签名,包括自定义修饰符,部分返回类型、 参数类型、 标记和非托管调用约定。Hide-by-name-and-signature considers all of the parts of the signature, including custom modifiers, return types, parameter types, sentinels, and unmanaged calling conventions. 这是二进制比较。This is a binary comparison.

  2. 为反射,属性和事件均由名称和签名隐藏。For reflection, properties and events are hide-by-name-and-signature. 如果具有 get 和 set 访问器的属性中的基类,但派生的类具有一个 get 访问器,派生的类属性隐藏基类属性,并且你将无法再访问基类上的资源库。If you have a property with both a get and a set accessor in the base class, but the derived class has only a get accessor, the derived class property hides the base class property, and you will not be able to access the setter on the base class.

  3. 自定义特性不是通用类型系统的一部分。Custom attributes are not part of the common type system.

数组或 COM 类型是不执行搜索,除非它们已加载到表中的可用类。Arrays or COM types are not searched for unless they have already been loaded into the table of available classes.

typeName 可将类型名称由限定其命名空间或程序集限定名,包括程序集名称规范。typeName can be the type name qualified by its namespace or an assembly-qualified name that includes an assembly name specification. 请参阅 AssemblyQualifiedNameSee AssemblyQualifiedName.

如果typeName包括命名空间而不是程序集名称,此方法搜索仅调用对象的程序集和 Mscorlib.dll,按该顺序。If typeName includes the namespace but not the assembly name, this method searches only the calling object's assembly and Mscorlib.dll, in that order. 如果使用的部分或完整的程序集名称完全限定类型名称,此方法搜索中指定的程序集。If typeName is fully qualified with the partial or complete assembly name, this method searches in the specified assembly. 如果该程序集具有强名称,则需要一个完整的程序集名称。If the assembly has a strong name, a complete assembly name is required.

AssemblyQualifiedName属性将返回包括嵌套的类型、 程序集名称和类型参数的完全限定的类型名称。The AssemblyQualifiedName property returns a fully qualified type name including nested types, the assembly name, and type arguments. 支持公共语言运行时的所有编译器将都发出嵌套类的简单名称和反射构造查询时,根据以下约定的重整的名称。All compilers that support the common language runtime will emit the simple name of a nested class, and reflection constructs a mangled name when queried, in accordance with the following conventions.

备注

在.NET Framework 2.0 版中,处理器体系结构添加到程序集标识,并可以指定为程序集名称字符串的一部分。In the .NET Framework version 2.0, processor architecture is added to assembly identity, and can be specified as part of assembly name strings. 例如,"ProcessorArchitecture = msil"。For example, "ProcessorArchitecture=msil". 但是,不包括在返回的字符串AssemblyQualifiedName属性,出于兼容性原因。However, it is not included in the string returned by the AssemblyQualifiedName property, for compatibility reasons. 此外可以通过创建加载类型AssemblyName对象并将其传递到的相应重载Load方法。You can also load types by creating an AssemblyName object and passing it to an appropriate overload of the Load method. 然后,可以使用Assembly.GetType方法来加载类型的程序集中。You can then use the Assembly.GetType method to load types from the assembly. 另请参阅 AssemblyName.ProcessorArchitectureSee also AssemblyName.ProcessorArchitecture.

分隔符Delimiter 含义Meaning
反斜杠 (\)Backslash (\) 转义符。Escape character.
反撇号 (')Backtick (`) 位于一个或多个数字表示的类型参数,位于末尾的泛型类型的名称数。Precedes one or more digits representing the number of type parameters, located at the end of the name of a generic type.
方括号 ([])Brackets ([]) 泛型类型参数列表,用于构造泛型类型; 括起来在类型参数列表中,则将程序集限定的类型。Enclose a generic type argument list, for a constructed generic type; within a type argument list, enclose an assembly-qualified type.
逗号 (,)Comma (,) 程序集名称前面。Precedes the Assembly name.
句点 (.)Period (.) 表示命名空间标识符。Denotes namespace identifiers.
加号 (+)Plus sign (+) 位于嵌套的类。Precedes a nested class.

例如,一个类的完全限定的名称可能如下所示:For example, the fully qualified name for a class might look like this:

TopNamespace.SubNameSpace.ContainingClass+NestedClass,MyAssembly  

如果命名空间为 TopNamespace.Sub+Namespace,则必须在字符串前面加一个转义字符加号 (+) (\) 以防止它被解释为嵌套分隔符。If the namespace were TopNamespace.Sub+Namespace, then the string would have to precede the plus sign (+) with an escape character (\) to prevent it from being interpreted as a nesting separator. 反射发出此字符串,如下所示:Reflection emits this string as follows:

TopNamespace.Sub\+Namespace.ContainingClass+NestedClass,MyAssembly  

一个"+ +"变为"\+\+",和一个"\"变为"\\"。A "++" becomes "\+\+", and a "\" becomes "\\".

可以保持此限定的名,并在以后使用它来加载TypeThis qualified name can be persisted and later used to load the Type. 若要搜索并加载Type,使用GetType使用类型名称只或用程序集限定的类型名称。To search for and load a Type, use GetType either with the type name only or with the assembly qualified type name. GetType 使用类型名称只会查找Type中调用方的程序集,然后在系统程序集。GetType with the type name only will look for the Type in the caller's assembly and then in the System assembly. GetType 使用程序集限定的类型名称将寻找Type在任何程序集中。GetType with the assembly qualified type name will look for the Type in any assembly.

类型名称可能包含尾随字符表示的类型,如类型是引用类型、 指针类型或数组类型有关的其他信息。Type names may include trailing characters that denote additional information about the type, such as whether the type is a reference type, a pointer type or an array type. 若要检索的类型名称而无需这些尾随字符,请使用t.GetElementType().ToString(),其中t的类型。To retrieve the type name without these trailing characters, use t.GetElementType().ToString(), where t is the type.

空格都是相关程序集名称之外的所有类型的名称组件中。Spaces are relevant in all type name components except the assembly name. 在程序集名称,分隔符前的空格相关,但是、 分隔符之后的空格将被忽略。In the assembly name, spaces before the ',' separator are relevant, but spaces after the ',' separator are ignored.

泛型类型的名称结尾反引号 (`) 跟数字表示的泛型类型参数的数目。The name of a generic type ends with a backtick (`) followed by digits representing the number of generic type arguments. 此名称重整的目的是允许编译器来支持在同一作用域中发生的泛型类型具有相同名称但具有不同数量的类型参数。The purpose of this name mangling is to allow compilers to support generic types with the same name but with different numbers of type parameters, occurring in the same scope. 例如,反射返回重整的名称Tuple1and元组2从泛型方法Tuple(Of T)Tuple(Of T0, T1)在 Visual Basic 中,或Tuple<T>和元组\<T0, T1>Visual C# 中。For example, reflection returns the mangled names Tuple1andTuple2 from the generic methods Tuple(Of T) and Tuple(Of T0, T1) in Visual Basic, or Tuple<T> and Tuple\<T0, T1> in Visual C#.

对于泛型类型,类型实参列表括在方括号内,并由逗号分隔的类型参数。For generic types, the type argument list is enclosed in brackets, and the type arguments are separated by commas. 例如,泛型Dictionary<TKey,TValue>具有两个类型参数。For example, a generic Dictionary<TKey,TValue> has two type parameters. 一个Dictionary<TKey,TValue>MyType类型的密钥与String可能表示,如下所示:A Dictionary<TKey,TValue> of MyType with keys of type String might be represented as follows:

System.Collections.Generic.Dictionary`2[System.String,MyType]  

若要指定类型实参列表内的某个程序集限定类型,请将放在括号内的程序集限定类型。To specify an assembly-qualified type within a type argument list, enclose the assembly-qualified type within brackets. 否则,单独的程序集限定名称的逗号解释为限制其他类型参数。Otherwise, the commas that separate the parts of the assembly-qualified name are interpreted as delimiting additional type arguments. 例如,Dictionary<TKey,TValue>MyTypeMyAssembly.dll,与键的类型从String,可能会按如下所示指定:For example, a Dictionary<TKey,TValue> of MyType from MyAssembly.dll, with keys of type String, might be specified as follows:

Type.GetType("System.Collections.Generic.Dictionary`2[System.String,[MyType,MyAssembly]]")  

备注

程序集限定的类型可以括在括号中,仅当它出现在类型参数列表。An assembly-qualified type can be enclosed in brackets only when it appears within a type parameter list. 在类型参数列表中搜索程序集限定和未限定类型的规则是限定和未限定的非泛型类型的规则相同。The rules for searching assemblies for qualified and unqualified types in type parameter lists are the same as the rules for qualified and unqualified nongeneric types.

可以为 null 的类型是泛型类型的一种特殊情况。Nullable types are a special case of generic types. 例如,一个可以为 nullInt32由字符串"System.Nullable'1[System.Int32]"表示。For example, a nullable Int32 is represented by the string "System.Nullable`1[System.Int32]".

备注

在C#, C++,和 Visual Basic 还可以获取可为 null 类型使用类型运算符。In C#, C++, and Visual Basic you can also get nullable types using type operators. 例如,可以为 nullBoolean返回类型typeof(Nullable<bool>)中C#,也可由Nullable<Boolean>::typeid在C++,并通过GetType(Nullable(Of Boolean))Visual Basic 中。For example, the nullable Boolean type is returned by typeof(Nullable<bool>) in C#, by Nullable<Boolean>::typeid in C++, and by GetType(Nullable(Of Boolean)) in Visual Basic.

下表显示了与使用的语法GetType为各种类型。The following table shows the syntax you use with GetType for various types.

若要获取To Get 使用Use
一个可以为 null Int32A nullable Int32 Type.GetType("System.Nullable`1[System.Int32]")
指向非托管的指针 MyTypeAn unmanaged pointer to MyType Type.GetType("MyType*")
非托管的指针到指向 MyTypeAn unmanaged pointer to a pointer to MyType Type.GetType("MyType**")
托管的指针或引用 MyTypeA managed pointer or reference to MyType Type.GetType("MyType&")Type.GetType("MyType&"). 请注意,与指针不同,引用仅限于一个级别。Note that unlike pointers, references are limited to one level.
父类和嵌套的类A parent class and a nested class Type.GetType("MyParentClass+MyNestedClass")
一维数组下限为 0A one-dimensional array with a lower bound of 0 Type.GetType("MyArray[]")
一维数组下限未知A one-dimensional array with an unknown lower bound Type.GetType("MyArray[*]")
N 维数组An n-dimensional array 在中总共 n-1 次括号逗号 (,)。A comma (,) inside the brackets a total of n-1 times. 例如,System.Object[,,]表示一个三维Object数组。For example, System.Object[,,] represents a three-dimensional Object array.
二维数组的数组A two-dimensional array's array Type.GetType("MyArray[][]")
矩形二维数组具有未知的下限A rectangular two-dimensional array with unknown lower bounds Type.GetType("MyArray[,]")
具有一个类型参数的泛型类型A generic type with one type argument Type.GetType("MyGenericType`1[MyType]")
具有两个类型参数的泛型类型A generic type with two type arguments Type.GetType("MyGenericType`2[MyType,AnotherType]")
具有两个程序集限定类型参数的泛型类型A generic type with two assembly-qualified type arguments Type.GetType("MyGenericType`2[[MyType,MyAssembly],[AnotherType,AnotherAssembly]]")
具有程序集限定类型参数的程序集限定的泛型类型An assembly-qualified generic type with an assembly-qualified type argument Type.GetType("MyGenericType`1[[MyType,MyAssembly]],MyGenericTypeAssembly")
一个其类型参数为具有两个类型参数的泛型类型的泛型类型A generic type whose type argument is a generic type with two type arguments Type.GetType("MyGenericType`1[AnotherGenericType`2[MyType,AnotherType]]")
另请参阅

GetType(String, Func<AssemblyName,Assembly>, Func<Assembly,String,Boolean,Type>) GetType(String, Func<AssemblyName,Assembly>, Func<Assembly,String,Boolean,Type>) GetType(String, Func<AssemblyName,Assembly>, Func<Assembly,String,Boolean,Type>) GetType(String, Func<AssemblyName,Assembly>, Func<Assembly,String,Boolean,Type>)

获取具有指定名称的类型,(可选)提供自定义方法以解析程序集和该类型。Gets the type with the specified name, optionally providing custom methods to resolve the assembly and the type.

public:
 static Type ^ GetType(System::String ^ typeName, Func<System::Reflection::AssemblyName ^, System::Reflection::Assembly ^> ^ assemblyResolver, Func<System::Reflection::Assembly ^, System::String ^, bool, Type ^> ^ typeResolver);
public static Type GetType (string typeName, Func<System.Reflection.AssemblyName,System.Reflection.Assembly> assemblyResolver, Func<System.Reflection.Assembly,string,bool,Type> typeResolver);
static member GetType : string * Func<System.Reflection.AssemblyName, System.Reflection.Assembly> * Func<System.Reflection.Assembly, string, bool, Type> -> Type
Public Shared Function GetType (typeName As String, assemblyResolver As Func(Of AssemblyName, Assembly), typeResolver As Func(Of Assembly, String, Boolean, Type)) As Type

参数

typeName
String String String String

要获取的类型的名称。The name of the type to get. 如果提供了 typeResolver 参数,则类型名称可以为 typeResolver 能够解析的任何字符串。If the typeResolver parameter is provided, the type name can be any string that typeResolver is capable of resolving. 如果提供了 assemblyResolver 参数,或者使用了标准类型解析,则除非该类型位于当前正在执行的程序集或 Mscorlib.dll 中(在这种情况下足以提供其命名空间所限定的类型名称),否则 typeName 必须为程序集限定的名称(请参见 AssemblyQualifiedName)。If the assemblyResolver parameter is provided or if standard type resolution is used, typeName must be an assembly-qualified name (see AssemblyQualifiedName), unless the type is in the currently executing assembly or in Mscorlib.dll, in which case it is sufficient to supply the type name qualified by its namespace.

assemblyResolver
Func<AssemblyName,Assembly>

一个方法,它定位并返回 typeName 中指定的程序集。A method that locates and returns the assembly that is specified in typeName. assemblyResolver 对象形式传递给 AssemblyName 的程序集名称。The assembly name is passed to assemblyResolver as an AssemblyName object. 如果 typeName 不包含程序集的名称,则不调用 assemblyResolverIf typeName does not contain the name of an assembly, assemblyResolver is not called. 如果未提供 assemblyResolver,则执行标准程序集解析。If assemblyResolver is not supplied, standard assembly resolution is performed.

警告 不要通过未知的或不受信任的调用方传递方法。Caution Do not pass methods from unknown or untrusted callers. 此操作可能会导致恶意代码特权提升。Doing so could result in elevation of privilege for malicious code. 仅使用你提供或者熟悉的方法。Use only methods that you provide or that you are familiar with.

typeResolver
Func<Assembly,String,Boolean,Type>

一个方法,它在由 typeName 或标准程序集解析返回的程序集中定位并返回 assemblyResolver 所指定的类型。A method that locates and returns the type that is specified by typeName from the assembly that is returned by assemblyResolver or by standard assembly resolution. 如果未提供任何程序集,则 typeResolver 方法可以提供一个程序集。If no assembly is provided, the typeResolver method can provide one. 该方法还采用一个参数以指定是否执行不区分大小写的搜索;false 传递给该参数。The method also takes a parameter that specifies whether to perform a case-insensitive search; false is passed to that parameter.

警告 不要通过未知的或不受信任的调用方传递方法。Caution Do not pass methods from unknown or untrusted callers.

返回

具有指定名称的类型,如果未找到该类型,则返回 nullThe type with the specified name, or null if the type is not found.

异常

调用了类初始值设定项,且该项引发了异常。A class initializer is invoked and throws an exception.

typeName 解析为类型名称和程序集名称时(例如,当简单类型名称包含未转义的特殊字符时),发生错误。An error occurs when typeName is parsed into a type name and an assembly name (for example, when the simple type name includes an unescaped special character).

- 或 --or- typeName 表示将指针类型、ByRef 类型或 Void 作为其类型参数之一的泛型类型。typeName represents a generic type that has a pointer type, a ByRef type, or Void as one of its type arguments.

- 或 --or- typeName 表示具有错误的类型参数数目的泛型类型。typeName represents a generic type that has an incorrect number of type arguments.

- 或 --or- typeName 表示泛型类型,并且其类型参数之一不满足相应类型参数的约束。typeName represents a generic type, and one of its type arguments does not satisfy the constraints for the corresponding type parameter.

typeName 表示 TypedReference 的数组。typeName represents an array of TypedReference.

找到了程序集或其依赖项之一,但无法加载它们。The assembly or one of its dependencies was found, but could not be loaded.

- 或 --or- typeName 包含无效的程序集名称。typeName contains an invalid assembly name.

- 或 --or- typeName 是一个不含类型名称的有效程序集名称。typeName is a valid assembly name without a type name.

程序集或其依赖项之一无效。The assembly or one of its dependencies is not valid.

- 或 --or- 此程序集使用高于当前所加载版本的公共语言运行时版本编译而成。The assembly was compiled with a later version of the common language runtime than the version that is currently loaded.

注解

此方法和有关的详细信息的使用方案assemblyResolvertypeResolver参数可在GetType(String, Func<AssemblyName,Assembly>, Func<Assembly,String,Boolean,Type>, Boolean, Boolean)方法重载。Usage scenarios for this method and details about the assemblyResolver and typeResolver parameters can be found in the GetType(String, Func<AssemblyName,Assembly>, Func<Assembly,String,Boolean,Type>, Boolean, Boolean) method overload.

备注

如果typeName找不到,在调用GetType(String, Func<AssemblyName,Assembly>, Func<Assembly,String,Boolean,Type>)方法将返回nullIf typeName cannot be found, the call to the GetType(String, Func<AssemblyName,Assembly>, Func<Assembly,String,Boolean,Type>) method returns null. 它不会引发异常。It does not throw an exception. 若要控制是否会引发异常,调用的重载GetType方法具有throwOnError参数。To control whether an exception is thrown, call an overload of the GetType method that has a throwOnError parameter.

调用此方法的重载相当于调用GetType(String, Func<AssemblyName,Assembly>, Func<Assembly,String,Boolean,Type>, Boolean, Boolean)方法重载并指定false有关throwOnErrorignoreCase参数。Calling this method overload is the same as calling the GetType(String, Func<AssemblyName,Assembly>, Func<Assembly,String,Boolean,Type>, Boolean, Boolean) method overload and specifying false for the throwOnError and ignoreCase parameters.

GetType(String, Func<AssemblyName,Assembly>, Func<Assembly,String,Boolean,Type>, Boolean) GetType(String, Func<AssemblyName,Assembly>, Func<Assembly,String,Boolean,Type>, Boolean) GetType(String, Func<AssemblyName,Assembly>, Func<Assembly,String,Boolean,Type>, Boolean) GetType(String, Func<AssemblyName,Assembly>, Func<Assembly,String,Boolean,Type>, Boolean)

获取具有指定名称的类型,指定在找不到该类型时是否引发异常,(可选)提供自定义方法以解析程序集和该类型。Gets the type with the specified name, specifying whether to throw an exception if the type is not found, and optionally providing custom methods to resolve the assembly and the type.

public:
 static Type ^ GetType(System::String ^ typeName, Func<System::Reflection::AssemblyName ^, System::Reflection::Assembly ^> ^ assemblyResolver, Func<System::Reflection::Assembly ^, System::String ^, bool, Type ^> ^ typeResolver, bool throwOnError);
public static Type GetType (string typeName, Func<System.Reflection.AssemblyName,System.Reflection.Assembly> assemblyResolver, Func<System.Reflection.Assembly,string,bool,Type> typeResolver, bool throwOnError);
static member GetType : string * Func<System.Reflection.AssemblyName, System.Reflection.Assembly> * Func<System.Reflection.Assembly, string, bool, Type> * bool -> Type
Public Shared Function GetType (typeName As String, assemblyResolver As Func(Of AssemblyName, Assembly), typeResolver As Func(Of Assembly, String, Boolean, Type), throwOnError As Boolean) As Type

参数

typeName
String String String String

要获取的类型的名称。The name of the type to get. 如果提供了 typeResolver 参数,则类型名称可以为 typeResolver 能够解析的任何字符串。If the typeResolver parameter is provided, the type name can be any string that typeResolver is capable of resolving. 如果提供了 assemblyResolver 参数,或者使用了标准类型解析,则除非该类型位于当前正在执行的程序集或 Mscorlib.dll 中(在这种情况下足以提供其命名空间所限定的类型名称),否则 typeName 必须为程序集限定的名称(请参见 AssemblyQualifiedName)。If the assemblyResolver parameter is provided or if standard type resolution is used, typeName must be an assembly-qualified name (see AssemblyQualifiedName), unless the type is in the currently executing assembly or in Mscorlib.dll, in which case it is sufficient to supply the type name qualified by its namespace.

assemblyResolver
Func<AssemblyName,Assembly>

一个方法,它定位并返回 typeName 中指定的程序集。A method that locates and returns the assembly that is specified in typeName. assemblyResolver 对象形式传递给 AssemblyName 的程序集名称。The assembly name is passed to assemblyResolver as an AssemblyName object. 如果 typeName 不包含程序集的名称,则不调用 assemblyResolverIf typeName does not contain the name of an assembly, assemblyResolver is not called. 如果未提供 assemblyResolver,则执行标准程序集解析。If assemblyResolver is not supplied, standard assembly resolution is performed.

警告 不要通过未知的或不受信任的调用方传递方法。Caution Do not pass methods from unknown or untrusted callers. 此操作可能会导致恶意代码特权提升。Doing so could result in elevation of privilege for malicious code. 仅使用你提供或者熟悉的方法。Use only methods that you provide or that you are familiar with.

typeResolver
Func<Assembly,String,Boolean,Type>

一个方法,它在由 typeName 或标准程序集解析返回的程序集中定位并返回 assemblyResolver 所指定的类型。A method that locates and returns the type that is specified by typeName from the assembly that is returned by assemblyResolver or by standard assembly resolution. 如果未提供任何程序集,则该方法可以提供一个程序集。If no assembly is provided, the method can provide one. 该方法还采用一个参数以指定是否执行不区分大小写的搜索;false 传递给该参数。The method also takes a parameter that specifies whether to perform a case-insensitive search; false is passed to that parameter.

警告 不要通过未知的或不受信任的调用方传递方法。Caution Do not pass methods from unknown or untrusted callers.

throwOnError
Boolean Boolean Boolean Boolean

如果为 true,则在找不到该类型时引发异常;如果为 false,则返回 nulltrue to throw an exception if the type cannot be found; false to return null. 指定 false 还会取消某些其他异常条件,但并不取消所有条件。Specifying false also suppresses some other exception conditions, but not all of them. 请参见“异常”部分。See the Exceptions section.

返回

具有指定名称的类型。The type with the specified name. 如果找不到该类型,则 throwOnError 参数指定是返回 null 还是引发异常。If the type is not found, the throwOnError parameter specifies whether null is returned or an exception is thrown. 在某些情况下,将引发异常,而不考虑 throwOnError 的值。In some cases, an exception is thrown regardless of the value of throwOnError. 请参见“异常”部分。See the Exceptions section.

异常

调用了类初始值设定项,且该项引发了异常。A class initializer is invoked and throws an exception.

throwOnErrortrue,且找不到该类型。throwOnError is true and the type is not found.

- 或 --or- throwOnError 的值为 true,且 typeName 包含无效字符,例如嵌入的制表符。throwOnError is true and typeName contains invalid characters, such as an embedded tab. - 或 --or- throwOnError 的值为 true,且 typeName 为空字符串。throwOnError is true and typeName is an empty string.

- 或 --or- throwOnError 的值为 true,且 typeName 表示大小无效的数组类型。throwOnError is true and typeName represents an array type with an invalid size.

- 或 --or- typeName 表示 TypedReference 的数组。typeName represents an array of TypedReference.

typeName 解析为类型名称和程序集名称时(例如,当简单类型名称包含未转义的特殊字符时),发生错误。An error occurs when typeName is parsed into a type name and an assembly name (for example, when the simple type name includes an unescaped special character).

- 或 --or- throwOnErrortrue,且 typeName 包含无效的语法(例如 "MyType[,*,]")。throwOnError is true and typeName contains invalid syntax (for example, "MyType[,*,]").

- 或 --or- typeName 表示将指针类型、ByRef 类型或 Void 作为其类型参数之一的泛型类型。typeName represents a generic type that has a pointer type, a ByRef type, or Void as one of its type arguments.

- 或 --or- typeName 表示具有错误的类型参数数目的泛型类型。typeName represents a generic type that has an incorrect number of type arguments.

- 或 --or- typeName 表示泛型类型,并且其类型参数之一不满足相应类型参数的约束。typeName represents a generic type, and one of its type arguments does not satisfy the constraints for the corresponding type parameter.

throwOnError 的值为 true,且未找到程序集或程序集的某个依赖项。throwOnError is true and the assembly or one of its dependencies was not found.

- 或 --or- typeName 包含无效的程序集名称。typeName contains an invalid assembly name.

- 或 --or- typeName 是一个不含类型名称的有效程序集名称。typeName is a valid assembly name without a type name.

找到了程序集或其依赖项之一,但无法加载它们。The assembly or one of its dependencies was found, but could not be loaded.

程序集或其依赖项之一无效。The assembly or one of its dependencies is not valid.

- 或 --or- 此程序集使用高于当前所加载版本的公共语言运行时版本编译而成。The assembly was compiled with a later version of the common language runtime than the version that is currently loaded.

注解

此方法和有关的详细信息的使用方案assemblyResolvertypeResolver参数可在GetType(String, Func<AssemblyName,Assembly>, Func<Assembly,String,Boolean,Type>, Boolean, Boolean)方法重载。Usage scenarios for this method and details about the assemblyResolver and typeResolver parameters can be found in the GetType(String, Func<AssemblyName,Assembly>, Func<Assembly,String,Boolean,Type>, Boolean, Boolean) method overload.

调用此方法的重载相当于调用GetType(String, Func<AssemblyName,Assembly>, Func<Assembly,String,Boolean,Type>, Boolean, Boolean)方法重载并指定falseignoreCase参数。Calling this method overload is the same as calling the GetType(String, Func<AssemblyName,Assembly>, Func<Assembly,String,Boolean,Type>, Boolean, Boolean) method overload and specifying false for the ignoreCase parameter.

GetType(String, Func<AssemblyName,Assembly>, Func<Assembly,String,Boolean,Type>, Boolean, Boolean) GetType(String, Func<AssemblyName,Assembly>, Func<Assembly,String,Boolean,Type>, Boolean, Boolean) GetType(String, Func<AssemblyName,Assembly>, Func<Assembly,String,Boolean,Type>, Boolean, Boolean) GetType(String, Func<AssemblyName,Assembly>, Func<Assembly,String,Boolean,Type>, Boolean, Boolean)

获取具有指定名称的类型,指定是否执行区分大小写的搜索,在找不到类型时是否引发异常,(可选)提供自定义方法以解析程序集和该类型。Gets the type with the specified name, specifying whether to perform a case-sensitive search and whether to throw an exception if the type is not found, and optionally providing custom methods to resolve the assembly and the type.

public:
 static Type ^ GetType(System::String ^ typeName, Func<System::Reflection::AssemblyName ^, System::Reflection::Assembly ^> ^ assemblyResolver, Func<System::Reflection::Assembly ^, System::String ^, bool, Type ^> ^ typeResolver, bool throwOnError, bool ignoreCase);
public static Type GetType (string typeName, Func<System.Reflection.AssemblyName,System.Reflection.Assembly> assemblyResolver, Func<System.Reflection.Assembly,string,bool,Type> typeResolver, bool throwOnError, bool ignoreCase);
static member GetType : string * Func<System.Reflection.AssemblyName, System.Reflection.Assembly> * Func<System.Reflection.Assembly, string, bool, Type> * bool * bool -> Type
Public Shared Function GetType (typeName As String, assemblyResolver As Func(Of AssemblyName, Assembly), typeResolver As Func(Of Assembly, String, Boolean, Type), throwOnError As Boolean, ignoreCase As Boolean) As Type

参数

typeName
String String String String

要获取的类型的名称。The name of the type to get. 如果提供了 typeResolver 参数,则类型名称可以为 typeResolver 能够解析的任何字符串。If the typeResolver parameter is provided, the type name can be any string that typeResolver is capable of resolving. 如果提供了 assemblyResolver 参数,或者使用了标准类型解析,则除非该类型位于当前正在执行的程序集或 Mscorlib.dll 中(在这种情况下足以提供其命名空间所限定的类型名称),否则 typeName 必须为程序集限定的名称(请参见 AssemblyQualifiedName)。If the assemblyResolver parameter is provided or if standard type resolution is used, typeName must be an assembly-qualified name (see AssemblyQualifiedName), unless the type is in the currently executing assembly or in Mscorlib.dll, in which case it is sufficient to supply the type name qualified by its namespace.

assemblyResolver
Func<AssemblyName,Assembly>

一个方法,它定位并返回 typeName 中指定的程序集。A method that locates and returns the assembly that is specified in typeName. assemblyResolver 对象形式传递给 AssemblyName 的程序集名称。The assembly name is passed to assemblyResolver as an AssemblyName object. 如果 typeName 不包含程序集的名称,则不调用 assemblyResolverIf typeName does not contain the name of an assembly, assemblyResolver is not called. 如果未提供 assemblyResolver,则执行标准程序集解析。If assemblyResolver is not supplied, standard assembly resolution is performed.

警告 不要通过未知的或不受信任的调用方传递方法。Caution Do not pass methods from unknown or untrusted callers. 此操作可能会导致恶意代码特权提升。Doing so could result in elevation of privilege for malicious code. 仅使用你提供或者熟悉的方法。Use only methods that you provide or that you are familiar with.

typeResolver
Func<Assembly,String,Boolean,Type>

一个方法,它在由 typeName 或标准程序集解析返回的程序集中定位并返回 assemblyResolver 所指定的类型。A method that locates and returns the type that is specified by typeName from the assembly that is returned by assemblyResolver or by standard assembly resolution. 如果未提供任何程序集,则该方法可以提供一个程序集。If no assembly is provided, the method can provide one. 该方法还采用一个参数以指定是否执行不区分大小写的搜索;ignoreCase 的值传递给该参数。The method also takes a parameter that specifies whether to perform a case-insensitive search; the value of ignoreCase is passed to that parameter.

警告 不要通过未知的或不受信任的调用方传递方法。Caution Do not pass methods from unknown or untrusted callers.

throwOnError
Boolean Boolean Boolean Boolean

如果为 true,则在找不到该类型时引发异常;如果为 false,则返回 nulltrue to throw an exception if the type cannot be found; false to return null. 指定 false 还会取消某些其他异常条件,但并不取消所有条件。Specifying false also suppresses some other exception conditions, but not all of them. 请参见“异常”部分。See the Exceptions section.

ignoreCase
Boolean Boolean Boolean Boolean

true 执行的搜索不区分大小写,则为 typeName;对 false 执行的搜索区分大小写,则为 typeNametrue to perform a case-insensitive search for typeName, false to perform a case-sensitive search for typeName.

返回

具有指定名称的类型。The type with the specified name. 如果找不到该类型,则 throwOnError 参数指定是返回 null 还是引发异常。If the type is not found, the throwOnError parameter specifies whether null is returned or an exception is thrown. 在某些情况下,将引发异常,而不考虑 throwOnError 的值。In some cases, an exception is thrown regardless of the value of throwOnError. 请参见“异常”部分。See the Exceptions section.

异常

调用了类初始值设定项,且该项引发了异常。A class initializer is invoked and throws an exception.

throwOnErrortrue,且找不到该类型。throwOnError is true and the type is not found.

- 或 --or- throwOnError 的值为 true,且 typeName 包含无效字符,例如嵌入的制表符。throwOnError is true and typeName contains invalid characters, such as an embedded tab. - 或 --or- throwOnError 的值为 true,且 typeName 为空字符串。throwOnError is true and typeName is an empty string.

- 或 --or- throwOnError 的值为 true,且 typeName 表示大小无效的数组类型。throwOnError is true and typeName represents an array type with an invalid size.

- 或 --or- typeName 表示 TypedReference 的数组。typeName represents an array of TypedReference.

typeName 解析为类型名称和程序集名称时(例如,当简单类型名称包含未转义的特殊字符时),发生错误。An error occurs when typeName is parsed into a type name and an assembly name (for example, when the simple type name includes an unescaped special character).

- 或 --or- throwOnErrortrue,且 typeName 包含无效的语法(例如 "MyType[,*,]")。throwOnError is true and typeName contains invalid syntax (for example, "MyType[,*,]").

- 或 --or- typeName 表示将指针类型、ByRef 类型或 Void 作为其类型参数之一的泛型类型。typeName represents a generic type that has a pointer type, a ByRef type, or Void as one of its type arguments.

- 或 --or- typeName 表示具有错误的类型参数数目的泛型类型。typeName represents a generic type that has an incorrect number of type arguments.

- 或 --or- typeName 表示泛型类型,并且其类型参数之一不满足相应类型参数的约束。typeName represents a generic type, and one of its type arguments does not satisfy the constraints for the corresponding type parameter.

throwOnError 的值为 true,且未找到程序集或程序集的某个依赖项。throwOnError is true and the assembly or one of its dependencies was not found.

找到了程序集或其依赖项之一,但无法加载它们。The assembly or one of its dependencies was found, but could not be loaded.

- 或 --or- typeName 包含无效的程序集名称。typeName contains an invalid assembly name.

- 或 --or- typeName 是一个不含类型名称的有效程序集名称。typeName is a valid assembly name without a type name.

程序集或其依赖项之一无效。The assembly or one of its dependencies is not valid.

- 或 --or- 此程序集使用高于当前所加载版本的公共语言运行时版本编译而成。The assembly was compiled with a later version of the common language runtime than the version that is currently loaded.

注解

使用此方法的重载和及其关联的重载 (GetType(String, Func<AssemblyName,Assembly>, Func<Assembly,String,Boolean,Type>)GetType(String, Func<AssemblyName,Assembly>, Func<Assembly,String,Boolean,Type>, Boolean)) 来替换的默认实现GetType具有更灵活的实现方法。Use this method overload and its associated overloads (GetType(String, Func<AssemblyName,Assembly>, Func<Assembly,String,Boolean,Type>) and GetType(String, Func<AssemblyName,Assembly>, Func<Assembly,String,Boolean,Type>, Boolean)) to replace the default implementation of the GetType method with more flexible implementations. 通过提供你自己的方法来解析类型名称和包含它们的程序集的名称,可以执行以下操作:By providing your own methods that resolve type names and the names of the assemblies that contain them, you can do the following:

  • 控制从加载一种类型的程序集的版本。Control which version of an assembly a type is loaded from.

  • 提供查找不包括程序集名称的类型名称的另一个位置。Provide another place to look for a type name that does not include an assembly name.

  • 加载使用部分程序集名称的程序集。Load assemblies using partial assembly names.

  • 返回的子类System.Type不通过公共语言运行时 (CLR) 创建的。Return subclasses of System.Type that are not created by the common language runtime (CLR).

例如,在版本容错序列化此方法,可使用部分名称搜索"best fit"程序集。For example, in version-tolerant serialization this method enables you to search for a "best fit" assembly by using a partial name. 另一个重载GetType方法需要一个程序集限定类型名称,其中包括的版本号。Other overloads of the GetType method require an assembly-qualified type name, which includes the version number.

类型系统的备用实现可能需要返回的子类System.Type不通过 CLR 创建的; 所有类型的其他重载返回GetType方法是运行时类型。Alternate implementations of the type system may need to return subclasses of System.Type that are not created by the CLR; all types that are returned by other overloads of the GetType method are runtime types.

用法说明Usage Notes

此方法的重载和及其关联的重载解析typeName到的类型和名称的程序集名称,然后解析该名称。This method overload and its associated overloads parse typeName into the name of a type and the name of an assembly, and then resolve the names. 程序集名称的解析早类型名称的解析,因为必须在程序集的上下文中解析类型名称。Resolution of the assembly name occurs before resolution of the type name, because a type name must be resolved in the context of an assembly.

备注

如果您不熟悉的程序集限定类型名称的概念,请参阅AssemblyQualifiedName属性。If you are unfamiliar with the concept of assembly-qualified type names, see the AssemblyQualifiedName property.

如果typeName不是一个程序集限定的名称,跳过程序集解析。If typeName is not an assembly-qualified name, assembly resolution is skipped. 可以在 Mscorlib.dll 或当前正在执行的程序集的上下文中解析未限定的类型名称或你可以根据需要提供中的程序集typeResolver参数。Unqualified type names can be resolved in the context of Mscorlib.dll or the currently executing assembly, or you can optionally provide an assembly in the typeResolver parameter. 包括或忽略程序集名称,用于为中的表显示不同类型的名称解析的效果混合名称解析部分。The effects of including or omitting the assembly name for different kinds of name resolution are displayed as a table in the Mixed Name Resolution section.

常规使用注意事项:General usage notes:

  • 不要传递到方法assemblyResolvertypeResolver如果它们来自未知或不受信任的调用方。Do not pass methods to assemblyResolver or typeResolver if they come from unknown or untrusted callers. 仅使用你提供或者熟悉的方法。Use only methods that you provide or that you are familiar with.

    注意

    使用来自未知或不受信任的调用方的方法可能导致提升权限的恶意代码。Using methods from unknown or untrusted callers could result in elevation of privilege for malicious code.

  • 如果省略assemblyResolver和/或typeResolver参数、 的值throwOnError参数传递给执行默认解析的方法。If you omit the assemblyResolver and/or typeResolver parameters, the value of the throwOnError parameter is passed to the methods that perform the default resolution.

  • 如果throwOnErrortrue,此方法将引发TypeLoadExceptiontypeResolver返回null,和一个FileNotFoundExceptionassemblyResolver返回nullIf throwOnError is true, this method throws a TypeLoadException when typeResolver returns null, and a FileNotFoundException when assemblyResolver returns null.

  • 此方法不会捕获引发的异常assemblyResolvertypeResolverThis method does not catch exceptions thrown by assemblyResolver and typeResolver. 你负责解析程序方法通过引发任何异常。You are responsible for any exceptions that are thrown by the resolver methods.

解析程序集Resolving Assemblies

assemblyResolver方法接收AssemblyName对象,生成的分析中包含的字符串的程序集名称typeNameThe assemblyResolver method receives an AssemblyName object, which is produced by parsing the string assembly name that is included in typeName. 如果typeName不包含程序集名称,assemblyResolver不会调用并null传递给typeResolverIf typeName does not contain an assembly name, assemblyResolver is not called and null is passed to typeResolver.

如果assemblyResolver不提供、 标准程序集探测用于查找程序集。If assemblyResolver is not supplied, standard assembly probing is used to locate the assembly. 如果assemblyResolver提供,则GetType方法不会执行标准探测,这种情况下,您必须确保,在assemblyResolver可以处理将传递给它的所有程序集。If assemblyResolver is provided, the GetType method does not do standard probing; in that case you must ensure that your assemblyResolver can handle all the assemblies you pass to it.

assemblyResolver方法应返回null如果无法解析的程序集。The assemblyResolver method should return null if the assembly cannot be resolved. 如果assemblyResolver将返回nulltypeResolver不会调用,并且不进行其他处理发生; 此外,如果throwOnErrortrueFileNotFoundException引发。If assemblyResolver returns null, typeResolver is not called and no further processing occurs; additionally, if throwOnError is true, a FileNotFoundException is thrown.

如果AssemblyName传递给assemblyResolver是部分名称、 一个或多个其部件是nullIf the AssemblyName that is passed to assemblyResolver is a partial name, one or more of its parts are null. 例如,如果它具有无版本Version属性是nullFor example, if it has no version, the Version property is null. 如果Version属性,CultureInfo属性,并GetPublicKeyToken方法都返回null,然后提供的程序集简单名称。If the Version property, the CultureInfo property, and the GetPublicKeyToken method all return null, then only the simple name of the assembly was supplied. assemblyResolver方法可以使用或忽略程序集名称的所有部分。The assemblyResolver method can use or ignore all parts of the assembly name.

为中的表显示不同的程序集解析选项的作用混合名称解析部分中的,对于简单和程序集限定类型名称。The effects of different assembly resolution options are displayed as a table in the Mixed Name Resolution section, for simple and assembly-qualified type names.

解析类型Resolving Types

如果typeName未指定一个程序集的名称,typeResolver始终调用。If typeName does not specify an assembly name, typeResolver is always called. 如果typeName指定一个程序集的名称,typeResolver仅在成功解析的程序集名称时调用。If typeName specifies an assembly name, typeResolver is called only when the assembly name is successfully resolved. 如果assemblyResolver或标准程序集探测返回nulltypeResolver不调用。If assemblyResolver or standard assembly probing returns null, typeResolver is not called.

typeResolver方法接收三个参数:The typeResolver method receives three arguments:

  • 要搜索的程序集或null如果typeName不包含程序集名称。The assembly to search or null if typeName does not contain an assembly name.

  • 简单类型的名称。The simple name of the type. 对于嵌套类型,这是最外面的包含类型。In the case of a nested type, this is the outermost containing type. 对于泛型类型,这是泛型类型的简单名称。In the case of a generic type, this is the simple name of the generic type.

  • 一个布尔值,是true如果类型名称的大小写将被忽略。A Boolean value that is true if the case of type names is to be ignored.

该实现确定这些自变量的使用的方式。The implementation determines the way these arguments are used. typeResolver方法应返回null如果无法解析类型。The typeResolver method should return null if it cannot resolve the type. 如果typeResolver将返回nullthrowOnErrortrue,此重载GetType引发TypeLoadExceptionIf typeResolver returns null and throwOnError is true, this overload of GetType throws a TypeLoadException.

为中的表显示不同的类型解析选项的作用混合名称解析部分中的,对于简单和程序集限定类型名称。The effects of different type resolution options are displayed as a table in the Mixed Name Resolution section, for simple and assembly-qualified type names.

解析嵌套的类型Resolving Nested Types

如果typeName是嵌套的类型,只有名称的最外层包含类型传递给typeResolverIf typeName is a nested type, only the name of the outermost containing type is passed to typeResolver. typeResolver返回此类型GetNestedType方法以递归方式调用之前的最内部的嵌套的类型已得到解决。When typeResolver returns this type, the GetNestedType method is called recursively until the innermost nested type has been resolved.

解析泛型类型Resolving Generic Types

GetType称为以递归方式来解析泛型类型:首先,若要解决的泛型类型本身,以及之后如何解析其类型参数。The GetType is called recursively to resolve generic types: First to resolve the generic type itself, and then to resolve its type arguments. 如果类型参数是泛型,GetType被递归调用来解析其类型参数中,依次类推。If a type argument is generic, GetType is called recursively to resolve its type arguments, and so on.

组合assemblyResolvertypeResolver提供必须能够解析此递归的所有级别。The combination of assemblyResolver and typeResolver that you provide must be capable of resolving all levels of this recursion. 例如,假设您提供assemblyResolver,它控制的加载MyAssemblyFor example, suppose you supply an assemblyResolver that controls the loading of MyAssembly. 假设你想要解析的泛型类型Dictionary<string, MyType>(Dictionary(Of String, MyType)在 Visual Basic 中)。Suppose you want to resolve the generic type Dictionary<string, MyType> (Dictionary(Of String, MyType) in Visual Basic). 您可能会传递以下泛型类型名称:You might pass the following generic type name:

"System.Collections.Generic.Dictionary`2[System.String,[MyNamespace.MyType, MyAssembly]]"  

请注意,MyType是只有程序集限定类型参数。Notice that MyType is the only assembly-qualified type argument. 名称Dictionary<TKey,TValue>String类不是程序集限定。The names of the Dictionary<TKey,TValue> and String classes are not assembly-qualified. typeResolver必须能够处理的程序集或null,因为它将接收null有关Dictionary<TKey,TValue>StringYour typeResolver must be able handle either an assembly or null, because it will receive null for Dictionary<TKey,TValue> and String. 它可以处理这种情况下,通过调用的重载GetType采用一个字符串,因为两者都未限定的类型名称是在 Mscorlib.dll 中的方法:It can handle that case by calling an overload of the GetType method that takes a string, because both of the unqualified type names are in Mscorlib.dll:

Type t = Type.GetType(test,
                      (aName) => aName.Name == "MyAssembly" ? 
                          Assembly.LoadFrom(@".\MyPath\v5.0\MyAssembly.dll") : null,
                      (assem, name, ignore) => assem == null ? 
                          Type.GetType(name, false, ignore) : 
                              assem.GetType(name, false, ignore)
                     ); 

assemblyResolver不会调用方法的字典类型和字符串类型,因为这些类型名称不是程序集限定。The assemblyResolver method is not called for the dictionary type and the string type, because those type names are not assembly-qualified.

现在假设,而不是System.String,第一种泛型自变量类型是YourType,从YourAssembly:Now suppose that instead of System.String, the first generic argument type is YourType, from YourAssembly:

"System.Collections.Generic.Dictionary`2[[YourNamespace.YourType, YourAssembly, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null], [MyNamespace.MyType, MyAssembly]]"  

由于此程序集是 Mscorlib.dll 和当前正在执行的程序集都不,不能解决YourType不含程序集限定的名称。Because this assembly is neither Mscorlib.dll nor the currently executing assembly, you cannot resolve YourType without an assembly-qualified name. 因为你assemblyResolve将以递归方式调用,它必须能够处理这种情况。Because your assemblyResolve will be called recursively, it must be able to handle this case. 而不是返回null以外的其他程序集MyAssembly,它现在的执行程序集加载成使用提供AssemblyName对象。Instead of returning null for assemblies other than MyAssembly, it now performs an assembly load using the supplied AssemblyName object.

Type t2 = Type.GetType(test,
                       (aName) => aName.Name == "MyAssembly" ? 
                           Assembly.LoadFrom(@".\MyPath\v5.0\MyAssembly.dll") : 
                           Assembly.Load(aName),
                       (assem, name, ignore) => assem == null ? 
                           Type.GetType(name, false, ignore) : 
                               assem.GetType(name, false, ignore), true
                      ); 

返回到用法说明Back to Usage Notes.

解析带有特殊字符的类型名称Resolving Type Names with Special Characters

某些字符在程序集限定名称中具有特殊含义。Certain characters have special meanings in assembly-qualified names. 如果简单类型名称包含这些字符,字符会导致分析错误时的简单名称是程序集限定名的一部分。If a simple type name contains these characters, the characters cause parsing errors when the simple name is part of an assembly-qualified name. 若要避免出现分析错误,必须转义反斜杠的特殊字符之前可以将传递到的程序集限定名称,GetType方法。To avoid the parsing errors, you must escape the special characters with a backslash before you can pass the assembly-qualified name to the GetType method. 例如,如果名为某种Strange]Type,方括号前必须按如下所示添加转义符: Strange\]TypeFor example, if a type is named Strange]Type, the escape character must be added ahead of the square bracket as follows: Strange\]Type.

备注

使用此类特殊字符的名称不能在 Visual Basic 或 C# 中,创建,但可以使用 Microsoft 中间语言 (MSIL),或发出动态程序集创建。Names with such special characters cannot be created in Visual Basic or C#, but can be created by using Microsoft intermediate language (MSIL) or by emitting dynamic assemblies.

下表显示了类型名称的特殊字符。The following table shows the special characters for type names.

字符Character 含义Meaning
, (逗号), (comma) 程序集限定名称的分隔符。Delimiter for assembly-qualified names.
[] (方括号)[] (square brackets) 作为后缀对,指示数组类型;作为分隔符对,包含泛型参数列表和程序集限定名称。As a suffix pair, indicates an array type; as a delimiter pair, encloses generic argument lists and assembly-qualified names.
& (与号)& (ampersand) 作为后缀,表示一种类型是引用类型。As a suffix, indicates that a type is a reference type.
* (星号)* (asterisk) 作为后缀,表示一种类型是指针类型。As a suffix, indicates that a type is a pointer type.
+ (加号)+ (plus) 对嵌套类型分隔符。Delimiter for nested types.
\ (反斜杠)\ (backslash) 转义符。Escape character.

属性,如AssemblyQualifiedName返回正确转义的字符串。Properties such as AssemblyQualifiedName return correctly escaped strings. 必须传递到的正确转义的字符串GetType方法。You must pass correctly escaped strings to the GetType method. 依次GetType方法将传递到正确转义后的名称typeResolver和到默认类型解析方法。In turn, the GetType method passes correctly escaped names to typeResolver and to the default type resolution methods. 如果需要进行比较将名称传递给中的非转义名称typeResolver,必须删除转义符。If you need to compare a name to an unescaped name in typeResolver, you must remove the escape characters.

返回到用法说明Back to Usage Notes.

混合的名称解析Mixed Name Resolution

下表总结了之间的交互assemblyResolvertypeResolver,和类型名称和程序集名称中的所有组合的默认名称解析typeName:The following table summarizes the interactions between assemblyResolver, typeResolver, and default name resolution, for all combinations of type name and assembly name in typeName:

类型名称的内容Contents of type name 程序集冲突解决程序方法Assembly resolver method 类型解析程序方法Type resolver method 结果Result
程序集类型type, assembly nullnull nullnull 等效于调用Type.GetType(String, Boolean, Boolean)方法重载。Equivalent to calling the Type.GetType(String, Boolean, Boolean) method overload.
程序集类型type, assembly 提供provided nullnull assemblyResolver 返回程序集,则返回null如果无法解析程序集。assemblyResolver returns the assembly or returns null if it cannot resolve the assembly. 如果程序集解析,Assembly.GetType(String, Boolean, Boolean)方法重载用于从程序集加载该类型; 否则,将不会尝试解析的类型。If the assembly is resolved, the Assembly.GetType(String, Boolean, Boolean) method overload is used to load the type from the assembly; otherwise, there is no attempt to resolve the type.
程序集类型type, assembly nullnull 提供provided 等效于将转换到的程序集名称AssemblyName对象并调用Assembly.Load(AssemblyName)方法重载来获取程序集。Equivalent to converting the assembly name to an AssemblyName object and calling the Assembly.Load(AssemblyName) method overload to get the assembly. 如果程序集解析,则将它传递到typeResolver; 否则为typeResolver不调用,并且没有任何进一步尝试解析类型。If the assembly is resolved, it is passed to typeResolver; otherwise, typeResolver is not called and there is no further attempt to resolve the type.
程序集类型type, assembly 提供provided 提供provided assemblyResolver 返回程序集,则返回null如果无法解析程序集。assemblyResolver returns the assembly or returns null if it cannot resolve the assembly. 如果程序集解析,则将它传递到typeResolver; 否则为typeResolver不调用,并且没有任何进一步尝试解析类型。If the assembly is resolved, it is passed to typeResolver; otherwise, typeResolver is not called and there is no further attempt to resolve the type.
类型type null、 提供null, provided nullnull 等效于调用Type.GetType(String, Boolean, Boolean)方法重载。Equivalent to calling the Type.GetType(String, Boolean, Boolean) method overload. 由于未提供的程序集名称,将搜索只有 Mscorlib.dll 和当前正在执行的程序集。Because the assembly name is not provided, only Mscorlib.dll and the currently executing assembly are searched. 如果assemblyResolver提供,它将被忽略。If assemblyResolver is provided, it is ignored.
类型type null、 提供null, provided 提供provided typeResolver 调用时,和null传递的程序集。typeResolver is called, and null is passed for the assembly. typeResolver 可以从任何程序集,包括程序集加载为目的提供一种类型。typeResolver can provide a type from any assembly, including assemblies it loads for the purpose. 如果assemblyResolver提供,它将被忽略。If assemblyResolver is provided, it is ignored.
程序集assembly null、 提供null, provided null、 提供null, provided 一个FileLoadException引发,因为程序集名称会进行分析,就好像程序程序集限定类型名称。A FileLoadException is thrown, because the assembly name is parsed as if it were an assembly-qualified type name. 这会导致无效的程序集名称。This results in an invalid assembly name.

返回到:使用注意事项解析程序集解析类型Back to: Usage Notes, Resolving Assemblies, Resolving Types.

适用于