Sunday, September 27, 2015

Checking Whether a Dependency is Available in .NET

It may be desirable to have a .NET program that can use a library but can also function without it. Such programs are made slightly difficult to write by the fact that .NET assemblies must produce a list at compile-time of what other assemblies they depend upon. If one of those assemblies cannot be loaded at run-time, problems occur.

Fortunately, the errors are normal exceptions and can be caught and handled like any other, if you're careful. The exception for a missing dependency will be thrown when a method that references a type in the missing assembly is entered. Therefore, the presence of the optional library can be checked by wrapping in a try/catch block a call to a short method that uses one of the library's types. If the library is missing, an exception will be thrown, but execution can continue as long as you don't call any methods that involve the missing types.

No comments:

Post a Comment