[英]Assembly loaded from Resources raises Could not load file or assembly
我正在编写一个应该创建一些快捷方式的简单应用程序。 为此,我使用添加为嵌入式资源的Interop.IWshRuntimeLibrary
。
在课堂上我打电话给
Assembly.Load(Resources.Interop_IWshRuntimeLibrary);
我也尝试过:
AppDomain.CurrentDomain.Load(Resources.Interop_IWshRuntimeLibrary);
当我在VisualStudio输出窗口中构建应用程序时,我看到程序集已加载:
加载“ Interop.IWshRuntimeLibrary”。
但是,当我尝试在该程序集中使用对象时,它给了我这个例外:
“无法加载文件或程序集“ Interop.IWshRuntimeLibrary,版本= 1.0.0.0,区域性=中性,PublicKeyToken =空”。
它不是您想的那么简单,如果Visualstudio说已加载引用,则意味着在构建过程中已加载了项目的引用。 它与您要实现的目标无关。
最简单的方法是绑定到程序集解析失败时调用的AppDomain.AssemblyResolve事件 :
AppDomain currentDomain = AppDomain.CurrentDomain;
currentDomain.AssemblyResolve += new ResolveEventHandler(MyResolveEventHandler);
然后:
private Assembly MyResolveEventHandler(object sender,ResolveEventArgs args)
{
Assembly rtn=null;
//Check for the assembly names that have raised the "AssemblyResolve" event.
if(args.Name=="YOUR RESOURCE ASSEMBLY NAME")
{
//load from resource
Stream resxStream = Assembly.GetExecutingAssembly().GetManifestResourceStream("YOUR RESOURCE ASSEMBLY NAME");
byte[] buffer = new byte[resxStream.Length];
resxStream.Read(buffer, 0, resxStream.Length);
rtn = Assembly.Load(buffer);
}
return rtn;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.