How can I get a type from an assembly loaded from another folder?

I am using the following code:

Assembly.LoadFile("the assembly in another folder"); var type = Type.GetType("the full name of the type"); 

Although the assembly has already been loaded before this line of code, it always returns null in type .

PS: I passed the name with assembly qualifications, including the namespace, type name, assembly name, version, and public token.

+7
source share
4 answers

Type.GetType searches only the types in the calling assembly and types in the mscorlib.dll file if you do not pass the name assigned to the node. See here.

EDIT

It seems that Type.GetType can extract Type instances from assemblies in the context of the load. Assemblies loaded using LoadFile are in no context , and those loaded using LoadFrom are in the context of Load From; none of these contexts allow Type.GetType to be used, so resolution will not be executed. This article shows that Type information can be obtained for Assembly when the directory in which it is located is added as a trial privatePath, since then it will appear in the Load context, but will not work in other contexts.

+9
source

The "right" (MS recommended) way of doing this is when you should use Type.GetType(string) for types in assemblies that are not in the loading context, but in the context of loading or lack of context - this is binding to Appdomain.AssemblyResolve . The following code is relatively effective:

 // this resolver works as long as the assembly is already loaded // with LoadFile/LoadFrom or Load(string) / Load(byte[]) private static Assembly OnAssemblyResolve(object sender, ResolveEventArgs args) { var asm = (from a in AppDomain.CurrentDomain.GetAssemblies() where a.GetName().FullName == args.Name select a).FirstOrDefault(); if(asm == null) throw FileNotFoundException(args.Name); // this becomes inner exc return asm; } // place this somewhere in the beginning of your app: AppDomain.CurrentDomain.AssemblyResolve += OnAssemblyResolve; 

It seems a little more efficient to create a combination of AssemblyLoad / Resolve events to store the dictionary of loaded assemblies (use the assembly name as the name).

In Assembly.LoadFile
There are some serious drawbacks to using this method. According to MSDN :

LoadFile does not load files into the LoadFrom context and does not resolve dependencies using the load path, as the LoadFrom method does.

Therefore, if possible, do not use LoadFile. The resulting assembly is loaded into a context without a context, which has even more drawbacks than the load context. Use Assembly.LoadFrom instead, and the dependencies will automatically load from the boot path.

+4
source

The easiest way to do this is to simply catch the return value of Assembly.LoadFile in the variable and call GetType on it like this:

 Assembly assem = Assembly.LoadFile("assemblyLocation"); assem.GetType("typeName"); 

You may want to keep a link to this assembly if you want to often pull types from it or do what others have suggested, and make a more general method that will go through all the loaded assemblies.

+2
source

you can try this ....

Assembly.GetAssembly assumes that you have an instance of the type, and Type.GetType assumes that you have a fully qualified type name that includes the assembly name.

You can specify the path where the assembly is located.

If you only have a base type name, you need to do something more like this:

 public static String GetAssemblyNameContainingType(String typeName) { foreach (Assembly currentassembly in AppDomain.CurrentDomain.GetAssemblies()) { Type t = currentassembly.GetType(typeName, false, true); if (t != null) {return currentassembly.FullName;} } return "not found"; } 

It also assumes your type is declared at the root. You will need to provide a namespace or enclosing types in the name or repeat it in the same way.

+1
source

All Articles