noelicus
noelicus

Reputation: 15055

Is it possible to add a reference and not load the associated DLLs

I have a Winforms application that relies on a 3rd party SDK. I've included the .NET reference in the application, but don't always need/use it. If I try and execute the program on a machine without the DLLs it will not open at all: it won't even enter Main.

Is it possible have a reference but instruct it to only load the DLLs when required?

(PDFSharp (another reference I use) appears to only load when a PdfSharp method is called, which makes me wonder if it's something I can control.)

Edit...I can't see any 3rd party reference in the Program class, but just in case here it is:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;

namespace MyProgram
{
    static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main(string[] args)
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);

            try
            {
                Application.Run(new Main(args));
            }
            catch (Exception Ex)
            {
                MessageBox.Show(Ex.Message, "The program has quit :(", MessageBoxButtons.OK, MessageBoxIcon.Error);

                string TracefileContents = "Please email this file to [email protected]\n\n" + "Exception:\n" + Ex.Message + "\n\nStack trace:\n" + Ex.StackTrace.ToString();

                System.IO.File.WriteAllText("Issue Report " + DateTime.Now.ToString("yyyy-MM-dd-HH-mm-ss") + ".dat", TracefileContents);
            }
        }
    }
}

Upvotes: 1

Views: 190

Answers (2)

Moha Dehghan
Moha Dehghan

Reputation: 18443

Assemblies are only loaded when you start using some type from the assembly. Just having a reference to the assembly has no impact on the run-time behavior of the application

To be more accurate, CLR loads an assembly only when a it JIT-compiles a method that uses that type. This also includes using a type that derives from/implements one of the classes/interfaces of the assembly.

Even instantiating a class that have a field or property of a type from another assembly, does not enforce loading the assembly. Unless the field or property is being accessed in the class' constructor. For example when you set a field's value in its definition statment:

// `TypeFromAnotherAssembly` is loaded when the class is instantiated
class Test
{
    private TypeFromAnotherAssembly myField = CreateTypeFromAnotherAssembly();
}

compiler emits initialization code in the class' constructor. Then, according to the rule above, when the constructor is JIT-compiled (the class is instantiated for the first time), the CLR loads the assembly. This also includes setting the field's value to null:

// `TypeFromAnotherAssembly` is loaded when the class is instantiated
class Test
{
    private TypeFromAnotherAssembly myField = null;
}

This does not happen when you omit the initialization statement, although the result is exactly the same (the .NET runtime automatically initialized class fields to null or 0):

// `TypeFromAnotherAssembly` is NOT loaded when the class is instantiated
class Test
{
    private TypeFromAnotherAssembly myField;
}

You should be careful about static fields' initialization, because accessing the class in any way causes the initialization to occur.

Upvotes: 1

sara
sara

Reputation: 3934

.NET does that automatically, everything is loaded on demand by default.

Upvotes: 1

Related Questions