Click here to Skip to main content
12,998,712 members (112,078 online)
Click here to Skip to main content
Add your own
alternative version


34 bookmarked
Posted 1 Nov 2008

Load a User DLL implementing an AppIn interface

, 1 Nov 2008
Rate this:
Please Sign up or sign in to vote.
Loading a DLL containing implementations of an interface used as an extension to your application.


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


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:

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:

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:

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:

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.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.


  • First version.


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


About the Author

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.

You may also be interested in...


Comments and Discussions

GeneralThis is almost ok Pin
Sacha Barber2-Nov-08 1:03
mvpSacha Barber2-Nov-08 1:03 
GeneralRe: This is almost ok Pin
PIEBALDconsult2-Nov-08 3:10
memberPIEBALDconsult2-Nov-08 3:10 
GeneralRe: This is almost ok Pin
Sacha Barber3-Nov-08 4:55
mvpSacha Barber3-Nov-08 4:55 
GeneralRe: This is almost ok Pin
PIEBALDconsult3-Nov-08 16:00
memberPIEBALDconsult3-Nov-08 16:00 
GeneralRe: This is almost ok Pin
PIEBALDconsult26-Jan-09 6:34
memberPIEBALDconsult26-Jan-09 6:34 
GeneralRe: This is almost ok Pin
Sacha Barber26-Jan-09 9:31
mvpSacha Barber26-Jan-09 9:31 
GeneralRe: This is almost ok Pin
sam.hill2-Nov-08 5:10
membersam.hill2-Nov-08 5:10 
GeneralRe: This is almost ok Pin
Sacha Barber2-Nov-08 21:59
mvpSacha 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.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170622.1 | Last Updated 2 Nov 2008
Article Copyright 2008 by DanielWehrle
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid