如何试图捕捉组件未找到 [英] How to try and catch assembly not found

查看:127
本文介绍了如何试图捕捉组件未找到的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

OK,说我有一个这样的应用程序:

OK, say I have an application like this:

using System;
using AliensExist; // some DLL which can't be found...



我要的是,如果大会DLL AlienExist无法找到该应用程序将不会返回一个错误 - 而是被trycatched,也就是像...:

What I want is that if the assembly DLL AlienExist can't be found the application won't return an error - but rather be "trycatched" that is something like...:

using System;
try{
using AliensExist; // some DLL which can't be found...
} catch {}



怎么做?我知道,使用关键字以后无法使用......但我太懒惰,现在对其进行测试。

How to do it? I know the using keyword can't be used later...but i am way too lazy now to test it.

10倍!

推荐答案

您不能真正做到这一点的一个try / catch,但你可以做的是操作上的AppDomain的 AssemblyResolve 事件。

You can't really do that in a try/catch, but what you can do is handle the AssemblyResolve event on the AppDomain.

请参阅 http://msdn.microsoft.com/en-us/library/system.appdomain.assemblyresolve。 ASPX 进一步的细节。

但是,你至少需要得到你的代码编译。如果您正试图为参考,这可能会或可能不存在的程序集,需要动态地加载它,并从那里工作。

However, you will at least need to get your code compiling. If you are trying to "reference" an assembly which may or may not exist, you will need to dynamically load it and work from there.

这篇关于如何试图捕捉组件未找到的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆