.net - How to check programatically if an assembly reference exists in c#? -


i trying write small c#/.net library specific financial calculations.

i have written several classes depend on standard assemblies of .net framework. is, library these classes require nothing other .net framework (4.0 client).

now need additional class excel integration. class require additional assembly references related microsoft office , excel, respective object libraries.

my problem is: of users of library may have office , excel, not.

how can add additional class library both types of users can use library without getting errors?

more precisely: if user not have office , excel, user must able run classes excluding excel-related 1 without getting errors.

thanks help, selmar

i doing this. assemblies probed if exist/will work.

you can in many ways, of course, that's ended with:

  • extracted functionality of class interface (let's call it: iexcel) , added isavailable property it
  • implemented fake class implementing iexcel, , returning 'false' isavailable (and, of course, throwing notsupportedexception other methods).
  • create new assembly (important: new assembly) real implementation of iexcel
  • implement factory class 'create' decide 1 should returned , catch exception on resolve (or test)

assembly: myfacade

// interface public interface iexcel {     bool isavailable { get; }     // stuff }  // fake implementation public class fakeexcel: iexcel {     public isavailable { { return false; } }     // stuff should probalby throw notsupportedexception } 

assembly: myimplementation

// real implementation public class realexcel: iexcel {     private bool? _isavailable;      public bool isavailable     {         // return value if known, or perform quick test         { return (_isavailable = _isavailable ?? performquicktest()); }     }      private bool performquicktest()     {         try         {             // ... someting requires excel             // crash when cannot found/doesn't work         }         catch // (exception e)         {             return false;         }         return true;     } } 

assembly: myfacadefactory

public class excelfactory {     public static iexcel create()     {         // delay resolving assembly hiding creation in method         return try(newrealexcel) ?? new fakeexcel();     }      private static iexcel try(func<iexcel> generator)     {         try         {             var result = generator();             if (result.isavailable)                  return result;         }         catch // (exception e)         {             // not interested         }         return null; // didn't work exception or isavailable returned 'false'     }      // implemented delegate it's      // safer when put noinlining on     [methodimpl(methodimploptions.noinlining)]     private static iexcel newrealexcel()     {         return new realexcel();     } } 

what happen?

  • if have excel , myimplementation assembly can found, loaded, realexcel class created , used
  • if don't have excel have myimplementation assembly, loaded, realexcel class created, fail on 'performquicktest' fakeexcel used
  • if myimplementation assembly cannot found (you did not include it) fail when realexcel created in myfacade, fakeexcel used

you can of course things dynamic loading , reflection (less lines of code) little bit clunky use. i've found method reflection-less.


Comments

Popular posts from this blog

linux - Does gcc have any options to add version info in ELF binary file? -

android - send complex objects as post php java -

charts - What graph/dashboard product is facebook using in Dashboard: PUE & WUE -