大会从资源加薪加载无法加载文件或程序集 [英] 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,文化=中性公钥=空。
这不是那么简单,你想,如果VisualStudio中说,这是加载的参考表示,该项目的引用生成过程中被加载。它无关,与你所特林实现的。
最简单的方法是结合的 AppDomain.AssemblyResolve事件时调用的程序集的解析失败:
的AppDomain currentDomain = AppDomain.CurrentDomain;
currentDomain.AssemblyResolve + =新ResolveEventHandler(MyResolveEventHandler);
然后:
私人大会MyResolveEventHandler(对象发件人,ResolveEventArgs参数)
{
大会RTN = NULL;
//检查是否存在已提出的AssemblyResolve事件的程序集名称。
如果(args.Name ==你的资源集名称)
{
//负荷的资源
流resxStream = Assembly.GetExecutingAssembly()GetManifestResourceStream(你的资源集名称)。
byte []的缓冲区=新的字节[resxStream.Length]
resxStream.Read(缓冲液,0,resxStream.Length);
RTN =的Assembly.Load(缓冲液);
}
返回RTN;
}
I am writing a simple application that should create some shortcuts.
For this I use Interop.IWshRuntimeLibrary
that i add as an Embedded Resource.
On class init I call
Assembly.Load(Resources.Interop_IWshRuntimeLibrary);
I also tried:
AppDomain.CurrentDomain.Load(Resources.Interop_IWshRuntimeLibrary);
When I build the application in VisualStudio Output window I see that the assembly is loaded:
Loaded "Interop.IWshRuntimeLibrary".
But, when I try to use the objects in that assembly it gives me this exception:
"Could not load file or assembly "Interop.IWshRuntimeLibrary, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null".
It is not so simple as you think and if Visualstudio says that the reference is loaded it means that the references of the project is loaded during build. It has nothing to do with what you are tring to achieve.
the easiest way is to bind to AppDomain.AssemblyResolve Event that is called when the resolution of an assembly fails:
AppDomain currentDomain = AppDomain.CurrentDomain;
currentDomain.AssemblyResolve += new ResolveEventHandler(MyResolveEventHandler);
then:
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;
}
这篇关于大会从资源加薪加载无法加载文件或程序集的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!