Skip to content
February 25, 2008 / dranaxum

Dynamic load .NET dll files (Creating a plug-in system) – C#

Finally I found some spare time to write an article on my (this) blog.

In the last days I needed to write a plug-in system for one of my projects. I managed to write it in C# and decided to share it on my blog. It’s really simple to create as you will see:
//This is a C# Console Application Project. The code was written with .NET Framework 2.0

using System;
using System.Reflection;

namespace ConsoleApplication1
class Program
static void Main(string[] args)
if (args.Length >= 1)
object[] s;
if (args.Length >= 2)
s = new object[args.Length – 1];
Array.Copy(args, 1, s, 0, args.Length – 1);
s = new object[] { };

Console.WriteLine(“Loading dll…”);
Console.WriteLine(LoadDllMethod(“Namespace1.Class1”, “Method1”, args[0], s));
Console.WriteLine(“Dll method called!”);
}catch(Exception ex){Console.WriteLine(“Error:” + ex.ToString());}

static string LoadDllMethod(string title_class, string title_void, string path, object[] parameters)
Assembly u = Assembly.LoadFile(path);
Type t = u.GetType(title_class);
if (t != null)
MethodInfo m = t.GetMethod(title_void);
if (m != null)
if (parameters.Length >= 1)
object[] myparam = new object[1];
myparam[0] = parameters;
return (string)m.Invoke(null, myparam);
return (string)m.Invoke(null, null);
Exception ex = new Exception(“method/class not found”);
throw ex;

Off course you must change the code for specified type methods.

If you have 2 parameters for Method1 you must define the myparam Object as 2 length (object[] myparam=new myparams[2]), also you can get some exceptions if the Method1’s parameters are not objects so you must change myparam type.

Please take into account that the dll-s written in a .NET version major to the one the “invoker” was written in will not load.



Leave a Comment
  1. ilanhur / Sep 18 2008 9:35 am

    Thanks m8. I really needed something like that 🙂

  2. tomexou / Jan 4 2009 3:53 pm

    This article just tell how to load the specific method dynamically only, not for the whole DLL loading like general GAC. What I want is like DLL declaration in App.config.

  3. dranaxum / Jan 4 2009 4:57 pm

    Use DllImport for loading functions if you don’t want to use this method.
    (using System.Runtime.InteropServices;)

  4. Max / Feb 21 2009 2:48 pm

    Hi folks.
    I have a NET dll, there is a fucntion that is not exported but i need to find a way to call it from my C# app or by another dll?
    Is there a chance?

  5. Vinu Baby / Sep 3 2009 1:42 pm

    great work.. done

  6. Era / Sep 30 2009 4:20 pm

    I really wonder if it wouldn’t work with LoadLibrary(..) from WIN32 API, by invoke them and write an wrapper around it in C#.

    Why shouldn’t that work?
    The strange thing i cant find it when i search on it.

  7. Vince / Oct 3 2011 10:15 am

    Hi, thats greate!!!
    But how to unload the DLL that is invoked?


  1. Dynamic load .NET dll files (Creating a plug-in system) – C# | Ray's world with Ashley

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: