none
How to control loading of a referenced assembly RRS feed

  • Question

  • My program (standalone Windows Forms app) references an assembly in a DLL file.  When that DLL file is present in the same folder as the exe file, everything runs fine.

    The classes in that referenced assembly are not used all the time.  In fact, they are used only if a user decides to do certain optional things which most other users don't need most of the time.  Hence I am thinking of removing that DLL from most users.

    If I remove the DLL file, at some point in the program life cycle, an exception would arise with "Could not load file or assembly 'xxxxx, Version=.....' or one of its dependencies.  The system cannot find the file specified."  How can I trap the loading of the assembly so that I can give a better user experience by giving instructions to the user on how to get and install the DLL file for the few users who need the additional features or to simply ignore the absence of the assembly otherwise.
    Saturday, September 12, 2009 6:26 AM

Answers

  • Actually, I am wrong.

    ExternalIO.ReadFile is JIT compiled the first time it is called, ie at the line MyData mydata = new ExternalIO().ReadFile("xxx");

    • Marked as answer by K.Kong Saturday, September 12, 2009 8:37 AM
    Saturday, September 12, 2009 8:36 AM

All replies

  • How about wrapping the relevant code in a try{}catch(FileNotFoundException){} block?
    Saturday, September 12, 2009 7:22 AM
  • Thanks.  My problem is that I don't know who/what loads the DLL at when.  The exception occurs at some point before the actual reference to a class in that assembly.

    I put a try and catch around those methods that reference that assembly.  This didn't catch the exception.

    I put a try and catch around the method that instantiates the class containing the method that references that assembly, yes it caught the exception.  Will this always work?

    Thanks.

    Saturday, September 12, 2009 7:38 AM
  • >>I put a try and catch around the method that instantiates the class containing the method that references that assembly, yes it caught the exception

    It sounds to me like the class also references types in the assembly either in it's constructor, or declares a field of type contained in the assembly, ie, the exception occurs when the class is initialized. Is this the case? If yes then your second solution should be fine.
    Saturday, September 12, 2009 8:02 AM
  • Thanks.  I checked but the only reference to the DLL is in a method.  The code is as follows:


    My application:

    ...
     try 
     {
         MyData mydata = new ExternalIO().ReadFile("xxx");
      }
     catch {FileNotFoundException F)
     {
        //catches the assembly not found exeption here
     }

    ....

    public class ExternalIO
    {
        public MyData ReadFile(string Filename)
        {
           try
           {        
              ...
              OpenSource.Data data = ...;
              ...
           }
           catch
           {
               // exception not caught here if OpenSource.dll is missing
           }
        }


    -------------------------------------

    The DLL file contains:

    namespace OpenSource
    {
       public class Data
       {
            ...
       }
    }

    Saturday, September 12, 2009 8:18 AM
  • Actually, I am wrong.

    ExternalIO.ReadFile is JIT compiled the first time it is called, ie at the line MyData mydata = new ExternalIO().ReadFile("xxx");

    • Marked as answer by K.Kong Saturday, September 12, 2009 8:37 AM
    Saturday, September 12, 2009 8:36 AM