Dynamic Type Loading

Here is a very simple sample of dynamic type loading:

==== LoadLibrary.cs ==== 

using System;
using System.Reflection;

namespace LoadLibrary
{
    class Program
    {
        static void Main(string[] args)
        {
            Assembly ass = Assembly.LoadFile(AppDomain.CurrentDomain.BaseDirectory
                + @”.SimpleLib.dll”);
            Type type = ass.GetType(“SimpleLib.SimpleClass”);
            object newInstance = Activator.CreateInstance(type);
            Console.WriteLine(newInstance);
            Console.ReadLine();
        }
    }
}

=== SimpleClass.cs ===

namespace SimpleLib
{
    public class SimpleClass
    {
        public override string ToString()
        {
            return “I am a simple class”;
        }
    }
}

 

Build simple class into SimpleLib.dll

Build LoadLibrary into LoadLibrary.exe

Put SimpleLib.dll into the same directory as LoadLibrary.

 

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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