How to try and catch assembly not found

前端 未结 2 468
梦谈多话
梦谈多话 2021-01-02 06:00

OK, say I have an application like this:

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

What I want is that if the

2条回答
  •  迷失自我
    2021-01-02 06:21

    Old question, but I feel this info needs added:

    You can't try/catch a using statement, but you can try/catch the resolving of an assembly.

    You have even more control if you manually load assemblies as suggested by Mike Atlas.

    Also, you can manually verify an assembly before attempting to resolve by selecting from GetEntryAssembly.GetReferencedAssemblies() and comparing against something like AssemblyName.GetAssemblyName().

    Of course, you can use a simple File.Exists(AssemblyPath) in some cases, but the methods above would help verify additional assembly issues, such as version numbering or signing.

    AssemblyResolve is nice by offering an event based approach. Good if you've got high cyclomatic complexity and/or want to ride along with JIT, but less informative than an upfront verification.

    Be mindful of the fail-fast principal, and the extent to which assembly resolve errors may limit functionality.

提交回复
热议问题