Click here to Skip to main content
Click here to Skip to main content

Load a User DLL implementing an AppIn interface

By , 1 Nov 2008
Rate this:
Please Sign up or sign in to vote.

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:

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

About the Author

DanielWehrle
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 PinmvpSacha Barber2-Nov-08 1:03 
GeneralRe: This is almost ok PinmemberPIEBALDconsult2-Nov-08 3:10 
GeneralRe: This is almost ok PinmvpSacha Barber3-Nov-08 4:55 
GeneralRe: This is almost ok PinmemberPIEBALDconsult3-Nov-08 16:00 
GeneralRe: This is almost ok PinmemberPIEBALDconsult26-Jan-09 6:34 
GeneralRe: This is almost ok PinmvpSacha Barber26-Jan-09 9:31 
GeneralRe: This is almost ok Pinmembersam.hill2-Nov-08 5:10 
GeneralRe: This is almost ok PinmvpSacha Barber2-Nov-08 21:59 

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

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

| Advertise | Privacy | Mobile
Web01 | 2.8.140415.2 | Last Updated 2 Nov 2008
Article Copyright 2008 by DanielWehrle
Everything else Copyright © CodeProject, 1999-2014
Terms of Use
Layout: fixed | fluid