Click here to Skip to main content
11,790,385 members (40,184 online)
Click here to Skip to main content
Articles » Languages » C# » General » Downloads
Add your own
alternative version

A simple plug-in engine using Reflection

, 26 Sep 2012 CPOL 32.5K 881 97
This article describes how to create and use configurable plug-ins in your application.
using System;
using System.Linq;
using System.Configuration;
using System.Collections.Generic;

namespace PluginBase
    /// <summary>
    /// Base configuration class for a plugin
    /// </summary>
    public abstract class ConfigurationBase : ConfigurationSection
        /// <summary>
        /// Opens the configuration section with the specified type and name
        /// </summary>
        /// <param name="sectionName">Configuration section's name</param>
        /// <param name="configPath">Configuration file's path</param>
        /// <returns>Instance of the configuration section's class</returns>
        public static T Open<T>(string sectionName, string configPath) where T : ConfigurationBase, new()
            T instance = new T();
            if (configPath.EndsWith(".config", StringComparison.InvariantCultureIgnoreCase))
                configPath = configPath.Remove(configPath.Length - 7);
                Configuration config = ConfigurationManager.OpenExeConfiguration(configPath);
                /* section not found */
                if (config.GetSection(sectionName) == null)
                    config.Sections.Add(sectionName, instance);

                    foreach (ConfigurationProperty p in instance.Properties)
                        ((T)config.GetSection(sectionName)).SetPropertyValue(p, p.DefaultValue, true);

                    /* section already exists */
                    instance = (T)config.Sections[sectionName];
            catch (ConfigurationErrorsException)
                if (instance == null)
                    instance = new T();
            return instance;

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.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Vitaly Zhukov
Software Developer
Russian Federation Russian Federation
No Biography provided

You may also be interested in...

| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.1509028.1 | Last Updated 27 Sep 2012
Article Copyright 2012 by Vitaly Zhukov
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid