Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ignoring Version in an assembly-qualified name passed to Type.GetType()

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.

like image 234
sourcenouveau Avatar asked Jul 21 '09 13:07

sourcenouveau


People also ask

How to use 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 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.

How to Get type of assembly in c#?

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.


2 Answers

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);
}
like image 141
PJC Avatar answered Nov 26 '22 07:11

PJC


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.

like image 33
sourcenouveau Avatar answered Nov 26 '22 06:11

sourcenouveau