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.
Namespace: KGySoft.ReflectionAssembly: KGySoft.CoreLibraries (in KGySoft.CoreLibraries.dll) Version: 8.1.0
public static Type? ResolveType(
string typeName,
ResolveTypeOptions options = ResolveTypeOptions.TryToLoadAssemblies|ResolveTypeOptions.AllowPartialAssemblyMatch
)
Public Shared Function ResolveType (
typeName As String,
Optional options As ResolveTypeOptions = ResolveTypeOptions.TryToLoadAssemblies Or ResolveTypeOptions.AllowPartialAssemblyMatch
) As Type
public:
static Type^ ResolveType(
String^ typeName,
ResolveTypeOptions options = ResolveTypeOptions::TryToLoadAssemblies|ResolveTypeOptions::AllowPartialAssemblyMatch
)
static member ResolveType :
typeName : string *
?options : ResolveTypeOptions
(* Defaults:
let _options = defaultArg options ResolveTypeOptions.TryToLoadAssemblies|ResolveTypeOptions.AllowPartialAssemblyMatch
*)
-> Type
- 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.
TypeThe resolved
Type, or
if
the
ThrowError flag is not enabled in
options and
typeName could not be resolved with the provided
options.
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.
// 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");