Is it possible to get a Type
via Type.GetType()
when the assembly-qualified name passed into GetType()
specifies a different Version
than the version of the DLL that's actually loaded? If so, what is the behavior of GetType()
?
I want to get a Type
from an assembly regardless of what version the assembly is. I have a function which gets an assembly-qualified name as an argument:
Type someType = Type.GetType(someName);
The someName
value corresponds to the Type
I want to get, but it may not have the same Version
specified as what is loaded in my application.
To search for and load a Type, use GetType either with the type name only or with the assembly qualified type name. GetType with the type name only will look for the Type in the caller's assembly and then in the System assembly. GetType with the assembly qualified type name will look for the Type in any assembly.
GetType(String, Boolean, Boolean) Gets the Type object with the specified name in the assembly instance, with the options of ignoring the case, and of throwing an exception if the type is not found.
I've used this successfully:
Type type = Type.GetType(typeName, AssemblyResolver, null);
private static System.Reflection.Assembly AssemblyResolver(System.Reflection.AssemblyName assemblyName)
{
assemblyName.Version = null;
return System.Reflection.Assembly.Load(assemblyName);
}
In testing I found that GetType()
will return the proper type even if the currently-loaded assembly's version does not match the value in the assembly-qualified name's Version
field.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With