我已经处理了AssemblyResolve
事件,但仍然得到了FileNotFoundException
。我已经在类型初始值设定项中订阅了该事件,并在Assembly.LoadFrom
方法中调用了Main
方法:
class Program
{
static Program()
{
AppDomain.CurrentDomain.AssemblyResolve+=new ResolveEventHandler(DeployAssemblyHandler);
}
static void Main(string[] args)
{
try
{
System.Reflection.Assembly asm=Assembly.LoadFrom("AxInterop.SHDocVw.dll");
}
catch(Exception)
{
}
}
public static System.Reflection.Assembly DeployAssemblyHandler(object sender,ResolveEventArgs args)
{
Assembly asm = null;
string asmName = new AssemblyName(args.Name).Name;
string deployAssemblyDirPath = ""; // Common.AppUtil.InstallDir + AppUtil.DeployedAssemblyDir;
string[] deployDirectories = Directory.GetDirectories(deployAssemblyDirPath);
foreach(string deploy in deployDirectories)
{
try
{
asm = Assembly.LoadFrom(deploy + "\\" + asmName);
break;
}
catch (Exception ex) { }
}
return asm;
}
}
最佳答案
我遇到了类似的问题,最终使用了新的AppDomain AND(重要!)设置PrivateBinPath属性。关于另一个AppDomain的好处是,如果不再需要该程序集,则可以卸载该程序集。 (我的)示例代码是:
public class ProxyDomain : MarshalByRefObject
{
public bool TestAssembly(string assemblyPath)
{
Assembly testDLL = Assembly.LoadFile(assemblyPath);
//do whatever you need
return true;
}
}
AppDomainSetup ads = new AppDomainSetup();
ads.PrivateBinPath = Path.GetDirectoryName("C:\\some.dll");
AppDomain ad2 = AppDomain.CreateDomain("AD2", null, ads);
ProxyDomain proxy = (ProxyDomain)ad2.CreateInstanceAndUnwrap(typeof(ProxyDomain).Assembly.FullName, typeof(ProxyDomain).FullName);
bool isTdll = proxy.TestAssembly("C:\\some.dll");
AppDomain.Unload(ad2);
编辑:根据您的评论,您只是在寻找错误的事件处理程序。在您的情况下,应使用AppDomain.UnhandledException事件,因为AssemblyResolve事件具有不同的用途。
关于c# - 处理AssemblyResolve事件后仍收到异常FileNotFound,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/19250006/