How to make an external assembly available at runtime?

守給你的承諾、 提交于 2020-01-07 06:42:12

问题


I asked a question here and apparently the problem is where I can load an assembly using Reflection's Assembly.LoadFile or Assembly.LoadFrom, and get the type inside that assembly, the assembly is still not accessible in the whole application. So when WPF tries to resolve a type, it doesn't find that type because it doesn't find the assembly.

My question is, can I reference an assembly at runtime, so that it will be resolvable by WPF?


回答1:


A solution that works for me is handling the CurrentDomain.AssemblyResolve event

AppDomain.CurrentDomain.AssemblyResolve += 
    new ResolveEventHandler(OnAssemblyResolveFailure);

Assembly OnAssemblyResolveFailure(object sender, ResolveEventArgs args)
    {
        AssemblyName name = new AssemblyName(args.Name);
        Assembly assembly = .. //some logic here to load the assembly from assembly name
        return assembly;
    }

This way if the application cannot resolve an assembly name it will call your handler to find it



来源:https://stackoverflow.com/questions/4600688/how-to-make-an-external-assembly-available-at-runtime

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