locked
Are .NET assemblies only loaded on demand? RRS feed

  • Question

  • II have been researching the topic of dynamically loading .NET DLL's when I came across a posting which stated the following.

     ".NET assemblies are typically loaded on demand, meaning that the runtime shouldn't even try to load your dll if you don't call into it. You make sure that all calls to the Symbol library are restricted to a code path that is only executed after the type of device is validated."

    This indicates that it will not be necessary for my .NET application to dynamically load required DLL's because this functionality is already built in.  To be certain I have not misunderstood this quote I am asking for confirmation of the following. 

    Assume I have two .NET DLL's (i.e. each DLL/assembly is written specifically for .NET) named H1.dll and H2.dll.  The first, H1.dll supports one type of hardware and H2.dll supports a second type of hardware.  When my application is installed on a computer, the computer will contain either the first or second type of hardware but not both.  Similarly, only the DLL associated with the hardware will be installed on the computer.  In my .NET project, I use the IDE Common Properties Reference dialog to reference DLL's for both types of hardware.

    Are all of the following statements true? 

    1) At runtime, my appliation does not call/reference any functions/classes/information in either of the DLL's.  This means that neither of the 2 DLL's will be loaded. 

    2) At runtime, the user selects hardware type 1 as being installed in the computer.  My application only calls functions in H1.DLL and this automatically results in H1.DLL being loaded.   H2.DLL will not be loaded.

    3) At runtime, the user selects hardware type 1 as being installed in the computer.  My application incorrectly makes calls to functions in H2.DLL.  In theory, this will generate an exception which I can catch and respond accordingly to.

    I have limited experience working with manged code and .NET so please bear with me if these questions are simplistic.  

    Thanks,

    Ian

    .

     

    Thursday, March 1, 2007 9:46 PM

Answers

  • Yes, all of what you wrote sounds correct.

     

    Friday, March 2, 2007 10:17 AM
  • With the following code snipped (pulled from the MSDN library) you should be able to load an assembly on the fly by specifying its strong name:

    using System;
    using System.Reflection;

    class Class1
    {
        public static void Main()
        {
            Assembly SampleAssembly;
            // You must supply a valid fully qualified assembly name here.           
            SampleAssembly = Assembly.Load("Assembly text name, Version, Culture, PublicKeyToken");
            Type[] Types = SampleAssembly.GetTypes();
            // Display all the types contained in the specified assembly.
            foreach (Type oType in Types)
            {
                Console.WriteLine(oType.Name.ToString());
            }
        }
    }

    Sunday, March 4, 2007 10:13 AM

All replies

  • Yes, all of what you wrote sounds correct.

     

    Friday, March 2, 2007 10:17 AM
  • With the following code snipped (pulled from the MSDN library) you should be able to load an assembly on the fly by specifying its strong name:

    using System;
    using System.Reflection;

    class Class1
    {
        public static void Main()
        {
            Assembly SampleAssembly;
            // You must supply a valid fully qualified assembly name here.           
            SampleAssembly = Assembly.Load("Assembly text name, Version, Culture, PublicKeyToken");
            Type[] Types = SampleAssembly.GetTypes();
            // Display all the types contained in the specified assembly.
            foreach (Type oType in Types)
            {
                Console.WriteLine(oType.Name.ToString());
            }
        }
    }

    Sunday, March 4, 2007 10:13 AM