Click here to Skip to main content
15,881,248 members
Articles / Programming Languages / C#

Self Contained Assembly

Rate me:
Please Sign up or sign in to vote.
4.95/5 (13 votes)
13 Nov 2011CPOL9 min read 48K   1.5K   43  
Shows how deployment of dynamically loaded assembly can be simplified by reducing the assembly to single self contained file.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Windows.Forms;
using AssemblyLoader;
using PanelFactory;

namespace DemoClientApp
{
    public partial class MainForm : Form
    {
        AbstractPanelFactory factory = null;
        Panel panel;

        public MainForm()
        {
            InitializeComponent();
        }

        private void MainForm_Load(object sender, EventArgs e)
        {
            var binDebug = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);

            var panelFactoryPluginFile = SearchPanelFactoryPlugin(binDebug);

            if (panelFactoryPluginFile == string.Empty)
            {
                MessageBox.Show("No Plugin Found.");
            }

            Loader.ResolveAssembly = true;

            var pluginAssembly = Loader.LoadAssembly(panelFactoryPluginFile);

            foreach (var type in pluginAssembly.GetTypes())
            {
                if (type.IsAbstract == false)
                {
                    if (type.IsClass)
                    {
                        if (type.BaseType == typeof(AbstractPanelFactory))
                        {
                            factory = pluginAssembly.CreateInstance(type.FullName) as AbstractPanelFactory;
                        }
                    }
                }
            }
        }

        private string SearchPanelFactoryPlugin(string pluginFolder)
        {
            pluginFolder = pluginFolder + "\\PlugIns";
            var dllFiles = Directory.GetFiles(pluginFolder, "*.dll");

            foreach (var dll in dllFiles)
            {
                if (Path.GetFileName(dll) != "AbstractPanelFactory.dll")
                    if (dll.EndsWith("PanelFactory.dll"))
                    {
                        return dll;
                    }
            }

            return string.Empty;
        }

        private void btnGetPanel_Click(object sender, EventArgs e)
        {
            if (factory == null)
            {
                MessageBox.Show(" Unable to find plugin.");
            }

            if (colorDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                Color selectedColor = colorDialog1.Color;
                this.Controls.Remove(panel);
                panel = factory.GetPanel(selectedColor);

                panel.Dock = DockStyle.Fill;
                this.Controls.Add(panel);
            }
        }
    }
}

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
Software Developer Honeywell Automation India Ltd.
India India
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions