如何检查某个程序集是否存在?
我使用 Activator
根据程序集的短名称(例如“CustomModule”)实例化一个新类。它抛出 FileNotFoundException
,因为程序集不存在。 有没有办法检查某个程序集名称是否存在?
我使用以下代码:
System.Runtime.Remoting.ObjectHandle obj =
System.Activator.CreateInstance(assemblyName, className);
主要目标是测试程序集是否存在,而不是等待异常发生。
I'm using the Activator
to instantiate a new class based on the short name of an assembly (e.a. 'CustomModule'). It throws a FileNotFoundException
, because the assembly isn't there. Is there a way to check if a certain assembly name is present?
I'm using the following code:
System.Runtime.Remoting.ObjectHandle obj =
System.Activator.CreateInstance(assemblyName, className);
The main objective is to rather test for the presence of the assembly than to wait for the exception to occur.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
如果您注意到我对您的问题的评论,那么很明显,我并不能完全确定您想要或需要如何解决这个问题,但在我们有更详细的描述之前,我只能为您提供这个,希望它适合您的情况(关键是“搜索”程序集):.
NET 2.0 兼容代码
如果您想在尝试加载文件之前确定文件是否存在(一个很好的做法)然后,如果您知道它的名称并知道所需的位置,只需在解析程序集时尝试查找该文件即可:
If you'll notice my comment to your question it will be evident that I'm not rightly sure exactly how you want or need to go about this, but until we have a more elaborate description I can only offer you this in the hope it fits well to your situation (the key is in 'searching' the assemblies):
.NET 2.0 Compatible Code
If you want to determine whether or not the file exists before trying to load it (a good practice) then, given you have its name and know the desired location, simply try to find the file when the assembly is being resolved:
我认为最好不要试图避免异常。原因是,如果您有这样的代码,那么
在抢占式多任务操作系统中总是存在风险,即在检查和实际创建之间可能会添加/删除程序集。是的,我意识到这种风险很小,但我仍然认为异常变体看起来更好,因为它是原子的。
I think it is better not to try to avoid the exception. The reason is that if you have code like
there is always a risk in a pre-emptive multitasking OS that the assembly might be added/removed between the check and the actual creation. Yes, I realize this risk is minimal, but I still think the exception variant looks better because it is atomic.
缺少程序集肯定会构成异常,请尝试/捕获 FileNotFoundException 并根据您的逻辑处理情况。
Missing assembly definitely constitutes an exception, try/catch
FileNotFoundException
and handle the situation as per your logic.我希望它对将来的人有所帮助:
在您使用的每个外部 .dll 上,创建自己的唯一密钥,如下所示:
string key = "fjrj3288skckfktk4owoxkvkfk4o29dic";
然后,当您加载表单时,对于您拥有的每个外部 .dll,只需检查密钥是否存在,如下所示:
If(isMyLib.Variables.key == key)
// 继续
其他
// .dll 不存在或已损坏。
I hope it will help for someone in the future:
On every external .dll you are using, create its own uniqe key, like so:
string key = "fjrj3288skckfktk4owoxkvkfk4o29dic";
And then, when you load your form, for every single external .dll that you have got, simply check if the key is exists like so:
If(isMyLib.Variables.key == key)
// continue
else
// .dll does not exists or broken.