如何在运行时使外部程序集可用? [英] How to make an external assembly available at runtime?
问题描述
我在此处提问了显然问题是我可以使用Reflection的Assembly.LoadFile
或Assembly.LoadFrom
加载程序集,并在该程序集内获取类型,该程序集在整个应用程序中仍不可访问.因此,当WPF尝试解析类型时,它找不到该类型,因为它没有找到程序集.
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.
我的问题是,我可以在运行时引用程序集,以便WPF对其进行解析吗?
My question is, can I reference an assembly at runtime, so that it will be resolvable by WPF?
推荐答案
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
这篇关于如何在运行时使外部程序集可用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!