Click here to Skip to main content
15,891,033 members
Articles / Programming Languages / C#

Dynamic Generation of Interfaces Using a Pool of Objects

Rate me:
Please Sign up or sign in to vote.
4.64/5 (9 votes)
3 Oct 20075 min read 32.5K   147   34  
An article introducing a mechanism for on-the-fly interface generation
using System;
using System.Collections.Generic;
using System.Text;

namespace CommonInterfaces
{
    public abstract class PluginBase
    {
        public static bool DerivesFrom<T>(Type t) where T : class
        {
            if (t.BaseType == null || t == typeof(System.Object))
                return false;
            else if (t.BaseType.FullName == typeof(T).FullName)
                return true;
            else
                return DerivesFrom<T>(t.BaseType);
        }

        public static object[] LoadPlugins(string path)
        {
            System.Collections.Generic.List<object> retVal = new List<object>();

            foreach (string assemblyPath in System.IO.Directory.GetFiles(path, "*.dll"))
            {
                System.Reflection.Assembly assembly = System.Reflection.Assembly.LoadFrom(assemblyPath);
                System.Reflection.AssemblyName[] assemblyRefNames = assembly.GetReferencedAssemblies();

                foreach (System.Reflection.AssemblyName assemblyRefName in assemblyRefNames)
                    // if it doesn't work using the straightforward API...
                    if (System.Reflection.Assembly.Load(assemblyRefName.FullName) == null)
                        // HACK: assume that the assembly you're looking for is in the same folder as the proxy's path
                        try
                        {
                            string[] fileNames =
                                System.IO.Directory.GetFiles(
                                    System.IO.Path.GetDirectoryName(path),
                                    string.Format("*{0}.dll", assemblyRefName.Name)
                                );
                            if (fileNames.Length > 0)
                                System.Reflection.Assembly.LoadFrom(fileNames[0]);
                        }
                        catch (System.IO.FileNotFoundException)
                        {
                        }

                if (assembly != null)
                    foreach (Type type in assembly.GetTypes())
                        if (DerivesFrom<CommonInterfaces.PluginBase>(type))
                        {
                            System.Reflection.ConstructorInfo ci = type.GetConstructor(Type.EmptyTypes);
                            if (ci != null)
                                retVal.Add(ci.Invoke(new object[0]));
                        }
            }

            return retVal.ToArray();
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
Israel Israel
I am a software engineer, as well as a research student at The Hebrew University. I love coding, math, computer theory, reading, tennis, guitar, swimming, animals, and - most importantly - my wife and family.

Comments and Discussions