我试图制作一个EXE应用。为此,我有两个DLL在我的应用程序中标记为嵌入式资源。我可以成功使用AppDomain.CurrentDomain.AssemblyResolve += new ResolveEventHandler(myresolve)
方法在事件触发时返回一个程序集。
我遇到的困难是我的应用程序中的方法引用了多个程序集。当我使用上面的事件处理程序来加载和返回我的程序集时,我的应用程序在错误的程序集文件中查找一个方法,并得到一个"类型加载异常"。
因此,如何加载我的程序集,以便我的应用程序在适当的位置查找我可能使用的任何方法?
亲切的问候。
代码示例:
DLL解析:
AppDomain.CurrentDomain.AssemblyResolve += new ResolveEventHandler(ResolveDll)
static Assembly ResolveDll(object sender, ResolveEventArgs args)
{
using (var stream = Assembly.GetExecutingAssembly().GetManifestResourceStream("MyApplication.Resources.MyDll1.dll"))
{
byte[] assemblyData = new byte[stream.Length];
stream.Read(assemblyData, 0, assemblyData.Length);
return Assembly.Load(assemblyData); //Obviously resolves the first DLL, but how can I also resolve the second?
}
}
void SomeMethod()
{
MyDll1.Class.CallAMethod()
MyDll2.Class.CallAnotherMethod() //Type Load exception - Assembly Resolution told it to look in MyDll1.
}
答案 0 :(得分:0)
根据L.B.的建议,我使用args.name
尝试了一个简单的If / then块。这已经成功了。
if (args.Name == "MyDll1, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null")
{
using (var stream = Assembly.GetExecutingAssembly().GetManifestResourceStream("MyApplication.Resources.MyDLL1.dll"))
{
byte[] assemblyData = new byte[stream.Length];
stream.Read(assemblyData, 0, assemblyData.Length);
return Assembly.Load(assemblyData);
}
}
else if (args.Name == "MyDll2, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null")
{
using (var stream = Assembly.GetExecutingAssembly().GetManifestResourceStream("MyApplication.Resources.MyDll2.dll"))
{
byte[] assemblyData = new byte[stream.Length];
stream.Read(assemblyData, 0, assemblyData.Length);
return Assembly.Load(assemblyData);
}
}