Export (0) Print
Expand All

Type::GetType Method (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.

Namespace:  System
Assembly:  mscorlib (in mscorlib.dll)

public:
static Type^ GetType(
	String^ typeName, 
	Func<AssemblyName^, Assembly^>^ assemblyResolver, 
	Func<Assembly^, String^, bool, Type^>^ typeResolver, 
	bool throwOnError, 
	bool ignoreCase
)

Parameters

typeName
Type: System::String
The name of the type to get. If the typeResolver parameter is provided, the type name can be any string that typeResolver is capable of resolving. 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
Type: System::Func<AssemblyName, Assembly>
A method that locates and returns the assembly that is specified in typeName. The assembly name is passed to assemblyResolver as an AssemblyName object. If typeName does not contain the name of an assembly, assemblyResolver is not called. 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
Type: System::Func<Assembly, String, Boolean, Type>
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. 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
Type: System::Boolean
true to throw an exception if the type cannot be found; false to return nullptr. Specifying false also suppresses some other exception conditions, but not all of them. See the Exceptions section.
ignoreCase
Type: System::Boolean
true to perform a case-insensitive search for typeName, false to perform a case-sensitive search for typeName.

Return Value

Type: System::Type
The type with the specified name. If the type is not found, the throwOnError parameter specifies whether nullptr is returned or an exception is thrown. In some cases, an exception is thrown regardless of the value of throwOnError. See the Exceptions section.

ExceptionCondition
ArgumentNullException

typeName is nullptr.

TargetInvocationException

A class initializer is invoked and throws an exception.

TypeLoadException

throwOnError is true and the type is not found.

-or-

throwOnError is true and typeName contains invalid characters, such as an embedded tab.

-or-

throwOnError is true and typeName is an empty string.

-or-

throwOnError is true and typeName represents an array type with an invalid size.

-or-

typeName represents an array of TypedReference.

ArgumentException

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-

throwOnError is true and typeName contains invalid syntax (for example, "MyType[,*,]").

-or-

typeName represents a generic type that has a pointer type, a ByRef type, or Void as one of its type arguments.

-or-

typeName represents a generic type that has an incorrect number of type arguments.

-or-

typeName represents a generic type, and one of its type arguments does not satisfy the constraints for the corresponding type parameter.

FileNotFoundException

throwOnError is true and the assembly or one of its dependencies was not found.

FileLoadException

The assembly or one of its dependencies was found, but could not be loaded.

-or-

typeName contains an invalid assembly name.

-or-

typeName is a valid assembly name without a type name.

BadImageFormatException

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.

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.

  • Return subclasses of System::Type that are not created by the common language runtime (CLR).

For example, in version-tolerant serialization this method enables you to search for a "best fit" assembly by using a partial name. Other overloads of the GetType method require an assembly-qualified type name, which includes the version number.

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

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.

NoteNote

If you are unfamiliar with the concept of assembly-qualified type names, see the AssemblyQualifiedName property.

If typeName is not an assembly-qualified name, assembly resolution is skipped. 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:

  • 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.

    Caution noteCaution

    Using methods from unknown or untrusted callers could result in elevation of privilege for malicious code.

  • 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.

  • If throwOnError is true, this method throws a TypeLoadException when typeResolver returns nullptr, and a FileNotFoundException when assemblyResolver returns nullptr.

  • This 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

The assemblyResolver method receives an AssemblyName object, which is produced by parsing the string assembly name that is included in typeName. If typeName does not contain an assembly name, assemblyResolver is not called and nullptr is passed to typeResolver.

If assemblyResolver is not supplied, standard assembly probing is used to locate the assembly. 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.

The assemblyResolver method should return nullptr if the assembly cannot be resolved. If assemblyResolver returns nullptr, typeResolver is not called and no further processing occurs; additionally, if throwOnError is true, a FileNotFoundException is thrown.

If the AssemblyName that is passed to assemblyResolver is a partial name, one or more of its parts are nullptr. For example, if it has no version, the Version property is nullptr. If the Version property, the CultureInfo property, and the GetPublicKeyToken method all return nullptr, then only the simple name of the assembly was supplied. 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

If typeName does not specify an assembly name, typeResolver is always called. If typeName specifies an assembly name, typeResolver is called only when the assembly name is successfully resolved. If assemblyResolver or standard assembly probing returns nullptr, typeResolver is not called.

The typeResolver method receives three arguments:

  • The assembly to search or nullptr 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.

  • 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. The typeResolver method should return nullptr if it cannot resolve the type. If typeResolver returns nullptr 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

If typeName is a nested type, only the name of the outermost containing type is passed to typeResolver. When typeResolver returns this type, the GetNestedType method is called recursively until the innermost nested type has been resolved.

Resolving Generic Types

The GetType is called recursively to resolve generic types: First to resolve the generic type itself, and then to resolve its type arguments. If a type argument is generic, GetType is called recursively to resolve its type arguments, and so on.

The combination of assemblyResolver and typeResolver that you provide must be capable of resolving all levels of this recursion. For example, suppose you supply an assemblyResolver that controls the loading of MyAssembly. 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]]"

Notice that MyType is the only assembly-qualified type argument. The names of the Dictionary<TKey, TValue> and String classes are not assembly-qualified. Your typeResolver must be able handle either an assembly or nullptr, because it will receive nullptr for Dictionary<TKey, TValue> and String. 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)
                     ); 


The assemblyResolver method is not called for the dictionary type and the string type, because those type names are not assembly-qualified.

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]]"

Because this assembly is neither Mscorlib.dll nor the currently executing assembly, you cannot resolve YourType without an assembly-qualified name. Because your assemblyResolve will be called recursively, it must be able to handle this case. Instead of returning nullptr 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. 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. For example, if a type is named Strange]Type, the escape character must be added ahead of the square bracket as follows: Strange\]Type.

NoteNote

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.

Properties such as AssemblyQualifiedName return correctly escaped strings. You must pass correctly escaped strings to the GetType method. In turn, the GetType method passes correctly escaped names to typeResolver and to the default type resolution methods. 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

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

null

null

Equivalent to calling the Type::GetType(String, Boolean, Boolean) method overload.

type, assembly

provided

null

assemblyResolver returns the assembly or returns nullptr if it cannot resolve the assembly. 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

null

provided

Equivalent to converting the assembly name to an AssemblyName object and calling the Assembly::Load(AssemblyName) method overload to get the assembly. 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 returns the assembly or returns nullptr if it cannot resolve the assembly. 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, provided

null

Equivalent to calling the Type::GetType(String, Boolean, Boolean) method overload. Because the assembly name is not provided, only Mscorlib.dll and the currently executing assembly are searched. If assemblyResolver is provided, it is ignored.

type

null, provided

provided

typeResolver is called, and nullptr is passed for the assembly. typeResolver can provide a type from any assembly, including assemblies it loads for the purpose. If assemblyResolver is provided, it is ignored.

assembly

null, provided

null, provided

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.

.NET Framework

Supported in: 4

.NET Framework Client Profile

Supported in: 4

Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Community Additions

ADD
Show:
© 2014 Microsoft