Dynamic DLL loading with reflection


We often have to load different class libraries runtime. The code segment below solves this problem efficiently. Only you have to call the InvokeDllMethod of the DllLoader class with these parameters: first is the absolute path of the DLL you want to load, second is the name of the method you want to call and the third is the input parameters in an object array for the method of the DLL.

In the GetClassReference method we are searching for the method's class as a class which implements the IComparable interface. So in this case we suppose that the class which implements the IComparable interface has the method that we passed as the second parameter of InvokeDllMethod. Here you can search the class in different ways (e.g. name of class).

This solution is very efficient because we don't load the assembly or the class again if we loaded it before. We use two hashtables (one for classes, one for assemblies) to store references. Hashtables are efficient data structures because we can insert elements and search in the hashtable in constant time, so it is very fast. If we load same assemblies in every method calling, our program will be so redundant...
 
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
 
namespace SampleApp
{
    public static class DllLoader
    {
        private class DllLoaderClassInfo
        {
            public Type type;
            public object ClassObject;
 
            public DllLoaderClassInfo() { }
 
            public DllLoaderClassInfo(Type t, object c)
            {
                type = t;
                ClassObject = c;
            }
        }
 
        private static Hashtable AssemblyReferences = new Hashtable();
        private static Hashtable ClassReferences = new Hashtable();
 
        private static DllLoaderClassInfo GetClassReference(string AssemblyName)
        {
            if (ClassReferences.ContainsKey(AssemblyName) == false)
            {
                Assembly assembly;
                if (AssemblyReferences.ContainsKey(AssemblyName) == false)
                {
                    AssemblyReferences.Add(AssemblyName, assembly = Assembly.LoadFrom(AssemblyName));
                }
                else
                {
                    assembly = (Assembly)AssemblyReferences[AssemblyName];
                }
                foreach (Type type in assembly.GetTypes())
                {
                    bool implementsIComparable = typeof(IComparable).IsAssignableFrom(type);
                    if (implementsIComparable)
                    {
                        DllLoaderClassInfo ci = new DllLoaderClassInfo(type, Activator.CreateInstance(type));
                        ClassReferences.Add(AssemblyName, ci);
                        return (ci);
                    }
                }
                throw (new Exception("Could not instantiate class!"));
            }
            return ((DllLoaderClassInfo)ClassReferences[AssemblyName]);
        }
 
        public static object InvokeDllMethod(string assemblyName, string methodName, object[] args)
        {
            DllLoaderClassInfo ci = GetClassReference(assemblyName);
            object Result = ci.type.InvokeMember(methodName, BindingFlags.Default | BindingFlags.InvokeMethod, null, ci.ClassObject, args);
            return (Result);
        }
    }
 
    private class Program
    {
        private static void Main(string[] args)
        {
            object[] inputParams = new object[] { "abc", 1, DateTime.Now };
            DllLoader.InvokeDllMethod(@"D:\ClassLibraries\SampleClassLib.dll", "SampleMethod", inputParams);
        }
    }
}

Posted on 13:43 by csharper and filed under , , , , | 0 Comments »

Checking equality of arrays with a generic method


In this case we define the equality of two arrays as all array elements are pairwise equal.
The method below compares two arrays and it says that those are equal or not. The first and the second parameter is any kind of array. With the third boolean parameter you can give that method has to ignore position of elements in the array or not. In the method's generic parameter you have to give the type of the arrays. The two arrays must be the same type.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
 
namespace SampleApp
{
    public static class Utils
    {
        public static bool arraysAreEquals(Array a1, Array a2, bool ignorePositionOfElements)
        {
            if (a1 == null && a2 == null)
                return true;
            else if ((a1 == null && a2 != null) || (a1 != null && a2 == null))
                return false;
            else
            {
                if (a1.Length != a2.Length)
                    return false;
                else
                {
                    if (ignorePositionOfElements)
                    {
                        Array.Sort(a1);
                        Array.Sort(a2);
                    }
                    for (int i = 0; i < a1.Length; i++)
                    {
                        T x1 = (T)a1.GetValue(i);
                        T x2 = (T)a2.GetValue(i);
                        if (!x1.Equals(x2))
                            return false;
                    }
                }
            }
            return true;
        }
    }
 
    private class Program
    {
        private static void Main(string[] args)
        {
            string[] array1 = new string[] { "cba", "abc", "bca" };
            string[] array2 = new string[] { "bca", "cba", "abc" };
            Utils.arraysAreEquals<string>(array1, array2, false); // result is: false
            Utils.arraysAreEquals<string>(array1, array2, true);  // result is: true
        }
    }
}

Posted on 12:42 by csharper and filed under , , | 1 Comments »

Visual Studio 2010 Productivity Power Tools


The Visual Studio 2010 Productivity Power Tools is a set of extensions to Visual Studio 2010 Professional (and above) which improves developer productivity.

It has a lot of useful extensions:
  • Solution Navigator
  • Quick Access
  • Auto Brace Completion
  • Searchable and Reference Dialog
  • Highlight Current Line
  • HTML Copy
  • Triple Click
  • etc...
You can find it here:

Posted on 12:25 by csharper and filed under , , , , | 0 Comments »