Type.GetType fails to create type from already loaded assembly

牧云@^-^@ 提交于 2019-12-05 05:34:42

In order to understand why this doesn't work, you need to understand the issue of "load contexts". Type.GetType only looks at the "Load" context. The assembly you loaded into memory was in the "LoadFrom" context.

The only way to really get binds in the load context to see assemblies in the load-from context is to use the AssemblyResolve event and write code to return the correct assembly. The AssemblyResolve event fires right before the bind fails and all other assembly lookup did not succeed.

See the following links for more information about load contexts and issues that arise when using LoadFrom.

MSDN - http://msdn.microsoft.com/en-us/library/dd153782.aspx
AssemblyResolve - http://msdn.microsoft.com/en-us/library/system.appdomain.assemblyresolve.aspx
Suzanne Cook - http://blogs.msdn.com/b/suzcook/archive/2003/05/29/57143.aspx

If you can get the assembly using Assembly.LoadFrom then you can get the type by doing:

        Assembly assembly = Assembly.LoadFrom("whatever");
        Type myType = assembly.GetType("typeName")

The assembly.GetType has other overloads which you can find out about here

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!