I use the following code:
Assembly.LoadFile("the assembly in another folder");
var type = Type.GetType("the full name of the type");
Even though the assembly was already loaded before this line of code, it always returns null in type
.
PS: I did pass in the assembly qualified name, including namespace, type name, assembly name, version and public token.
Type.GetType
only searches the types in the calling assembly and the types in mscorlib.dll unless you pass the assembly qualified name of the type. See here.EDIT
It appears that
Type.GetType
is only able to retrieveType
instances from assemblies in the Load context. Assemblies loaded usingLoadFile
are in no context and those loaded usingLoadFrom
are in the Load From context; neither of these contexts allow you to useType.GetType
so the resolution will fail. This article shows thatType
information can be retrieved for anAssembly
when the directory it is in is added as a probing privatePath since it will then end up in the Load context but will fail in other contexts.The simplest way of doing it is to simply trap the return value of Assembly.LoadFile in a variable and call GetType on it like this:
You may want to consider keeping a reference to this assembly if you will want to pull types from it often, or do what others have suggested and make a more generic method that loops through all loaded assemblies.
The "proper" (MS recommended) way to do this, when you must use
Type.GetType(string)
on types in assemblies that are not in the load context but in the load-from or no-context context, is to bind to theAppdomain.AssemblyResolve
event. The following code is relatively efficient:It appears slightly more efficient to create a combination of the AssemblyLoad/Resolve events to keep a dictionary of the loaded assemblies (use the assembly-name as key).
On Assembly.LoadFile
There are some serious drawback on using this method. According to MSDN:
So, if possible, do not use LoadFile. The resulting assembly is loaded in the no-context context, which has even more drawbacks than the load-from context. Instead, use Assembly.LoadFrom and dependencies will be automatically loaded from the load path.
you can try this....
Assembly.GetAssembly assumes you have an instance of the type, and Type.GetType assumes you have the fully qualified type name which includes assembly name.
you can give path that assembly is located .....
If you only have the base type name, you need to do something more like this:
This also assumes your type is declared in the root. You would need to provide the namespace or enclosing types in the name, or iterate in the same manner.