ReflectorResolveType(String, ResolveTypeOptions) Method

Gets the Type with the specified typeName. When no assembly is defined in typeName, the type can be defined in any loaded assembly.

Definition

Namespace: KGySoft.Reflection
Assembly: KGySoft.CoreLibraries (in KGySoft.CoreLibraries.dll) Version: 8.1.0
C#
public static Type? ResolveType(
	string typeName,
	ResolveTypeOptions options = ResolveTypeOptions.TryToLoadAssemblies|ResolveTypeOptions.AllowPartialAssemblyMatch
)

Parameters

typeName  String
The type name as a string representation with or without assembly name.
options  ResolveTypeOptions  (Optional)
The options for resolving the type. This parameter is optional.
Default value: TryToLoadAssemblies, AllowPartialAssemblyMatch.

Return Value

Type
The resolved Type, or if the ThrowError flag is not enabled in options and typeName could not be resolved with the provided options.

Remarks

  Security Note

The default value of the options parameter allows loading assemblies if typeName is an assembly qualified name. This behavior is compatible with the Type.GetType method but can be a security risk if typeName is from an untrusted source, eg. file, user input, remote service, database, etc. In such cases do not enable the TryToLoadAssemblies flag so the type can be resolved from the already loaded assemblies.

typeName can be generic and may contain fully or partially defined assembly names.

typeName can contain generic parameter types in the format as they are returned by the TypeExtensions.GetName extension method.

Example

C#
// Here mscorlib types are defined without assembly, System.Uri is defined with fully qualified assembly name:
// it will be resolved only if the System.dll of the same version is already loaded.
var type = Reflector.ResolveType("System.Collections.Generic.Dictionary`2[System.String,[System.Uri, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]",
    ResolveTypeOptions.None);

// If System.dll is already loaded, then System.Uri will be resolved even if the loaded System.dll has a different version.
// If System.dll is not loaded, then null will be returned.
var type = Reflector.ResolveType("System.Collections.Generic.Dictionary`2[System.String,[System.Uri, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]",
    ResolveTypeOptions.AllowPartialAssemblyMatch);

// If System.dll is not loaded, then it will be tried to be loaded and it can have any version.
var type = Reflector.ResolveType("System.Collections.Generic.Dictionary`2[System.String,[System.Uri, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]",
    // this are actually the default options:
    ResolveTypeOptions.TryToLoadAssemblies | ResolveTypeOptions.AllowPartialAssemblyMatch);

// System.Uri is defined with partial assembly name. It will be resolved by default settings.
var type = Reflector.ResolveType("System.Collections.Generic.Dictionary`2[System.String,[System.Uri, System]]");

// All types are defined without assembly names. System.Uri will be resolved only if its assembly is already loaded.
var type = Reflector.ResolveType("System.Collections.Generic.Dictionary`2[System.String, System.Uri]");

// This is how a generic parameter of Dictionary<,> can be resolved. See also TypeExtensions.GetName.
var type = Reflector.ResolveType("!TKey:System.Collections.Generic.Dictionary`2");

See Also