Posted by: Zeeshan Amjad | December 21, 2009

Loading and using DLL at run time


Today i got a requirement to load DLL at run time then create object of one class in the DLL which doesn’t have any default constructor. It is important that we already know the name of the class as well as number of parameters pass to the constructor.

The main reason of this requirement is to make different DLL with similar interface but different implementation. At run time the program is going to load the appropriate DLL and then uses its class. Here is my proof of concept of this requirement. At first we are going to create one simple DLL with one parameterized constructor.

  1: using System;
  2: using System.Collections.Generic;
  3: using System.Linq;
  4: using System.Text;
  5: 
  6: namespace MyDll
  7: {
  8:     public class TimeValue
  9:     {        
 10:         public TimeValue(int year, int period, double interestRate, double presentValue)
 11:         {
 12:             Year = year;
 13:             Period = period;
 14:             InterestRate = interestRate;
 15:             PresentValue = presentValue;
 16: 
 17:             FutureValue = PresentValue * Math.Pow((1 + InterestRate / Period), Year * Period);
 18:         }
 19: 
 20:         public int Year
 21:         { get; set; }
 22: 
 23:         public int Period
 24:         { get; set; }
 25: 
 26:         public double InterestRate
 27:         { get; set; }
 28: 
 29:         public double PresentValue
 30:         { get; set; }
 31: 
 32:         public double FutureValue
 33:         { get; private set; }
 34:     }
 35: }
 36: 

We can use reflection to not only load the DLL but also objects as well as get the values of properties. Here is a client program of this DLL that uses it at run time.

  1: using System;
  2: using System.Collections.Generic;
  3: using System.Linq;
  4: using System.Text;
  5: using System.Reflection;
  6: 
  7: namespace MyDllClient
  8: {
  9:     class Program
 10:     {
 11:         static void Main(string[] args)
 12:         {
 13:             Assembly myDll = Assembly.LoadFile(@"C:\zamjad\Projects\MyDll\MyDll\bin\Debug\MyDll.dll");
 14: 
 15:             if (myDll != null)
 16:             {
 17:                 Type type = myDll.GetType("MyDll.TimeValue");
 18: 
 19:                 object[] param = new object[4];
 20:                 param[0] = 2;
 21:                 param[1] = 1;
 22:                 param[2] = 0.1;
 23:                 param[3] = 1000;
 24: 
 25:                 Object obj = Activator.CreateInstance(type, param);
 26: 
 27:                 if (obj != null)
 28:                 {
 29:                     PropertyInfo[] properties = type.GetProperties();
 30: 
 31:                     foreach (PropertyInfo pi in properties)
 32:                     {
 33:                         Object val = pi.GetValue(obj, null);
 34:                         Console.WriteLine("{0} = {1}", pi.Name, val);
 35:                     }
 36:                 }
 37:             }
 38:         }
 39:     }
 40: }
 41: 

This is the output of this program.

ReflectionOutput

Advertisements

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 )

Twitter picture

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

Facebook photo

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

Google+ photo

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

Connecting to %s

Categories

%d bloggers like this: