Byron Sommardahl
Byron Sommardahl

Reputation: 13012

Load Assembly into AppDomain from App.Config

I have a DLL that I need to be a part of the current AppDomain. Is there a way to signal the AppDomain to pick up dlls from some list in the app.config/web.config?

Upvotes: 0

Views: 964

Answers (1)

tsells
tsells

Reputation: 2771

You can put the name of the assembly in the app.config file and then load it up at run time using the Assembly.Load options and reflection. Here is a link to the MSDN article describing how to do this.

http://msdn.microsoft.com/en-us/library/25y1ya39.aspx

Basics

  1. Put name of assembly in app.config file
  2. Read entry using ConfigurationManager and get the name into a string in your program
  3. Pass the name of the assembly to the Assembly.Load method.

Example from the link:

public class Asmload0
{
    public static void Main()
    {
        // Use the file name to load the assembly into the current
        // application domain.
        Assembly a = Assembly.Load("example");
        // Get the type to use.
        Type myType = a.GetType("Example");
        // Get the method to call.
        MethodInfo myMethod = myType.GetMethod("MethodA");
        // Create an instance.
        object obj = Activator.CreateInstance(myType);
        // Execute the method.
        myMethod.Invoke(obj, null);
    }
}

Upvotes: 3

Related Questions