Click here to Skip to main content
15,867,568 members
Articles / Desktop Programming / Win32
Article

Load a User DLL implementing an AppIn interface

Rate me:
Please Sign up or sign in to vote.
3.53/5 (8 votes)
1 Nov 2008CPOL1 min read 45.5K   527   33   8
Loading a DLL containing implementations of an interface used as an extension to your application.

Introduction

Sometimes, you need to give a user of your application the chance to implement his own functions without recompiling the full application.

Background

For some projects, I needed the user to load their own implementations of an interface. These classes were used to collect and show some information in my application. The configuration was loaded by the runtime, and contained the name of the loaded and used classes.

So, it was needed to instantiate all these objects by Reflection during runtime.

Using the code

There are three projects in the .NET solution:

  • LoadFromDll -> Contains the main function loading the UserLibrary.
  • Interfaces -> Contains the interface used by the UserLibrary.
  • UserLibrary -> Contains the implementations of the user.

The Interface project is used as a resource in the UserLibrary project. It defines the interface IUserInterface:

C#
public interface IUserInterface
{
    String GetName();
    int Funktion(int a, int b);
}

IUserInterface is used by the implementations that are loaded at runtime. A sample of an implementation looks like:

C#
public class UserClass1 : Interfaces.IUserInterface
{
    public String GetName() { return "Add"; }
    public int Funktion(int a, int b) { return a + b; }
}

In the main functions, all classes implementing IUserInterface are collected:

C#
private static Dictionary<String, Type> nameTypeDict = new Dictionary<string, Type>(); 

[...].
Assembly ass = Assembly.LoadFrom(@"UserLibrary.dll");
foreach (Type t in ass.GetExportedTypes())
{
    //Get all classes implement IUserInterface
    if (t.GetInterface("IUserInterface", true)!=null)
    {
        Console.WriteLine("Found Type: {0}", t.Name);
        nameTypeDict.Add(t.Name, t);//Add to Dictonary
    }
}

Now, you are able to instantiate and use the implementations only by the name of the class:

C#
private static void ExecuteByName(String typeName, int a, int b)
{
    //Create Instance
    IUserInterface o = (IUserInterface)nameTypeDict[typeName].InvokeMember(null,        
                BindingFlags.DeclaredOnly |
                BindingFlags.Public | BindingFlags.NonPublic |
                BindingFlags.Instance | BindingFlags.CreateInstance, 
                null, null, null);
    //Print information and call function
    Console.WriteLine(o.GetName());
    Console.WriteLine(o.Funktion(a, b));
}

Points of interest

I use this code with a configuration file containing the assembly's file name and the order of the loaded classes.

History

  • First version.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer iTernity GmbH
Germany Germany
I studied computer science at the "Hochschule Furtwangen University" (Germany) and the "University of New Brunswick" (Kanada). I graduate in 2006 at the "Hochschule Furtwangen University" and work since that at the iTernity GmbH.

Comments and Discussions

 
GeneralThis is almost ok Pin
Sacha Barber2-Nov-08 1:03
Sacha Barber2-Nov-08 1:03 
GeneralRe: This is almost ok Pin
PIEBALDconsult2-Nov-08 3:10
mvePIEBALDconsult2-Nov-08 3:10 
GeneralRe: This is almost ok Pin
Sacha Barber3-Nov-08 4:55
Sacha Barber3-Nov-08 4:55 
GeneralRe: This is almost ok Pin
PIEBALDconsult3-Nov-08 16:00
mvePIEBALDconsult3-Nov-08 16:00 
GeneralRe: This is almost ok Pin
PIEBALDconsult26-Jan-09 6:34
mvePIEBALDconsult26-Jan-09 6:34 
GeneralRe: This is almost ok Pin
Sacha Barber26-Jan-09 9:31
Sacha Barber26-Jan-09 9:31 
No worries

Sacha Barber
  • Microsoft Visual C# MVP 2008
  • Codeproject MVP 2008
Your best friend is you.
I'm my best friend too. We share the same views, and hardly ever argue

My Blog : sachabarber.net

GeneralRe: This is almost ok Pin
sam.hill2-Nov-08 5:10
sam.hill2-Nov-08 5:10 
GeneralRe: This is almost ok Pin
Sacha Barber2-Nov-08 21:59
Sacha Barber2-Nov-08 21:59 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.