Testing for references availability programmatically?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Phoenix

    Testing for references availability programmatically?

    Hi,

    I'd like to do the following:

    My main application is a .exe, that uses a .dll included as a
    reference in the project.

    I'd like to know if it's possible for the .exe to physically test the
    presence of the .dll in its directory (i.e. theFile.exists) .
    So far, instructions to that extent have failed, because the Framework
    throws an exception before any code is executed.

    So, can I include such a check in the .exe, or do I have to catch the
    exception first thing? I don't think that would work, since as I've
    said the Framework crashes before, apparently, any code is executed in
    the .exe

    Thanks for any answer.
  • Spam Catcher

    #2
    Re: Testing for references availability programmaticall y?

    Phoenix <no@spam.comwro te in news:2693j29ebn atbt2jn85llb2af 3r3e9e3hh@
    4ax.com:
    I'd like to know if it's possible for the .exe to physically test the
    presence of the .dll in its directory (i.e. theFile.exists) .
    So far, instructions to that extent have failed, because the Framework
    throws an exception before any code is executed.
    >
    So, can I include such a check in the .exe, or do I have to catch the
    exception first thing? I don't think that would work, since as I've
    said the Framework crashes before, apparently, any code is executed in
    the .exe
    Yes it's possible - take a look at the System.Reflecti on namespace.

    The reflection namespace provides function to dynamically load DLLs from
    disk. Once you have the DLL loaded, you can scan through the assembly to
    see if the proper classes are in place, etc.

    Comment

    • Marc Gravell

      #3
      Re: Testing for references availability programmaticall y?

      Also note: you don't need to hard code the assemblies: look at:
      Assembly.GetExe cutingAssembly( ).GetReferenced Assemblies()
      This gives you the "what I reference" as AssemblyName instances,
      suitable for passing to Assembly.Load() ... so you can call that and see
      what you get back. Each only loads once, so this is safe to call. You
      may wish to resursively check each as you load it, but watch out that
      the 2 main MS libs are circularly referenced (each references the
      other), so watch you don't get into an infinite loop... for instance,
      keep a list of checked assemblies, and exclude references you have
      already checked.

      Also - this checking code *must* preceed the first attempt to use them.
      Additionally, you cannot use types from the referenced assemblies in
      the same method, as JIT works method-by-method.

      Marc

      Comment

      Working...