Click here to Skip to main content
15,886,770 members
Articles / Programming Languages / C#

Code generator, plug-in based

Rate me:
Please Sign up or sign in to vote.
3.89/5 (8 votes)
27 Dec 2006CPOL2 min read 35.8K   1.7K   55  
Another code generator, plug-in based.
using System;
using System.Collections.Generic;
using System.Text;
using Worker.Core.API;
using System.Data;
using System.Reflection;
namespace SimplePluginManager
{
    public class SimplePluginManager : Worker.Core.API.IPluginManager
    {
        DataTable pluginTable = new DataTable();
        DataSet dsManager = new DataSet();
        public SimplePluginManager()
        {
            DataColumn name = new DataColumn("Name");
            DataColumn version = new DataColumn("Version");
            DataColumn path = new DataColumn("Path");
            pluginTable.Columns.Add(name);
            pluginTable.Columns.Add(version);
            pluginTable.Columns.Add(path);
            dsManager.Tables.Add(pluginTable);
        }

        public List<IPlugin> GetAll()
        {
            System.Collections.Generic.List<IPlugin> arrayPlugin = new List<IPlugin>();
            foreach (DataRow dr in dsManager.Tables[0].Rows)
            {
                IPlugin plugin = new Plugin();
                plugin.Name = dr["Name"].ToString();
                plugin.Version = dr["Version"].ToString();
                plugin.Path = dr["Path"].ToString();
                arrayPlugin.Add(plugin);
            }
            return arrayPlugin;
        }
        public object Find(string name)
        {
            foreach (DataRow dr in dsManager.Tables[0].Rows)
            {
                if(name.Equals(dr["Name"].ToString()))
                {
                    IPlugin plugin = new Plugin();
                    plugin.Name = dr["Name"].ToString();
                    plugin.Version = dr["Version"].ToString();
                    plugin.Path = dr["Path"].ToString();
                    return plugin;
                }
            }
            return null;
        }
        public object Plug(IPlugin plugin,String type)
        { 
            Assembly asm = Assembly.Load(AssemblyName.GetAssemblyName(".\\Plugin\\" + plugin.Path));
            foreach (Type ftype in asm.GetTypes())
            {
                foreach (Type ifc in ftype.GetInterfaces())
                {
                    if (ifc.Name.Equals(type))
                    {
                        return Activator.CreateInstance(ftype);
                    }
                }
            }
            return null;
        }
        public bool Register(IPlugin info)
        {
            DataRow dr = dsManager.Tables[0].NewRow();
            dr["Name"] = info.Name;
            dr["Version"] = info.Version;
            dr["Path"] = info.Path;
            pluginTable.Rows.Add(dr);
            return true;
        }
        public void Persist(String source)
        {
            dsManager.WriteXml(source);
        }
        public void Load(String source)
        {
            dsManager.ReadXml(source);
        }
    }
}

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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Web Developer www.plugtecnologia.com
Brazil Brazil
He is a software developer based on Brazil. His main interests are Software architecture, Requirements Engineering and coding. He is a MCP and MCAD developer. If you want to get in contact, please send an e-mail to andre.betim@gmail.com or access his company website www.plugtecnologia.com

Comments and Discussions