10

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 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.

10x!

JaredPar
  • 733,204
  • 149
  • 1,241
  • 1,454
johny
  • 119
  • 2
  • 4
  • 3
    I would imagine the code would have to compile first right? – ChaosPandion Mar 04 '11 at 19:34
  • 1
    First of all `using` does not specify dll. It specifies used namespace which can be defined in multiple assemblies (dlls). – Ladislav Mrnka Mar 04 '11 at 19:37
  • The using statement doesn't add a reference to another assembly, your project build options do that. The fact that many assemblies contain a namespace named the same as the assembly is just a red herring. – Ben Voigt Mar 04 '11 at 19:37
  • It is the JIT compiler that causes the exception. Which happens when it tries to generate code for a type that's from the missing assembly. That requires that it reads the metadata from the assembly and that goes kaboom. You'll need to be 'ahead' by one method call to catch the exception, at least for the Microsoft jitter. Much sooner for Mono. You'll also need to suppress inlining with [MethodImpl] to avoid accidents in the Release build. Hard to get right, avoid this. – Hans Passant Mar 04 '11 at 19:54

2 Answers2

9

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

See http://msdn.microsoft.com/en-us/library/system.appdomain.assemblyresolve.aspx for further details.

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.

vcsjones
  • 138,677
  • 31
  • 291
  • 286
1

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.

Community
  • 1
  • 1
u8it
  • 3,956
  • 1
  • 20
  • 33