Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 528967
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T09:02:32+00:00 2026-05-13T09:02:32+00:00

I would like to have an xml config file as follows: <?xml version=1.0 encoding=utf-8

  • 0

I would like to have an xml config file as follows:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
    <section name="plugins" type="MyApp.PluginsConfiguration, MyApp"/>
</configSections>

<plugins>
    <use assembly="MyApp.Plugin1.dll" />
    <use assembly="MyApp.Plugin2.dll" />
</plugins>
</configuration>

How exactly do I arrange the interplay between the PluginsConfigurations, UsePluginCollection, and UsePlugin classes in my code?

I found this tutorial online but it would introduce an element inside of plugins that surrounds the use collection and I do not need this.

This is what I have so far but its not quite right

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T09:02:32+00:00Added an answer on May 13, 2026 at 9:02 am

    If MyApp.PluginsConfiguration is a ConfigurationSection, then you can define a new Class that inherits from ConfigurationElementCollection and make the new class a ConfigurationProperty of MyApp.PluginsConfiguration

    Check this article for some in-depth info about those types. I also blogged about having nested properties, but not specifically for collections.

    Edit: Here with some code. Given is this bit in the web.config:

    <configSections>
        <section name="plugins"
                 type="WebApplication1.PluginsConfiguration, WebApplication1"/>
    </configSections>
    <plugins>
        <use assembly="MyApp.Plugin1.dll"/>
        <use assembly="MyApp.Plugin2.dll"/>
    </plugins>
    

    Here is are the classes to make that happen. Note that there maybe NullReferenceExceptions that would need to be handled.

    namespace WebApplication1
    {
        public class PluginsConfiguration : ConfigurationSection
        {
            private static ConfigurationPropertyCollection properties;
            private static ConfigurationProperty propPlugins;
    
            static PluginsConfiguration()
            {
                propPlugins = new ConfigurationProperty(null, typeof(PluginsElementCollection),
                                                              null,
                                                              ConfigurationPropertyOptions.IsDefaultCollection);
                properties = new ConfigurationPropertyCollection { propPlugins };
            }
    
            protected override ConfigurationPropertyCollection Properties
            {
                get
                {
                    return properties;
                }
            }
    
            public PluginsElementCollection Plugins
            {
                get
                {
                    return this[propPlugins] as PluginsElementCollection;
                }
            }
        }
    
        public class PluginsElementCollection : ConfigurationElementCollection
        {
            public PluginsElementCollection()
            {
                properties = new ConfigurationPropertyCollection();
            }
    
            private static ConfigurationPropertyCollection properties;
    
            protected override ConfigurationPropertyCollection Properties
            {
                get
                {
                    return properties;
                }
            }
    
            public override ConfigurationElementCollectionType CollectionType
            {
                get
                {
                    return ConfigurationElementCollectionType.BasicMap;
                }
            }
    
            protected override string ElementName
            {
                get
                {
                    return "use";
                }
            }
    
            protected override ConfigurationElement CreateNewElement()
            {
                return new PluginsElement();
            }
    
            protected override object GetElementKey(ConfigurationElement element)
            {
                var elm = element as PluginsElement;
                if (elm == null) throw new ArgumentNullException();
                return elm.AssemblyName;
            }
        }
    
        public class PluginsElement : ConfigurationElement
        {
            private static ConfigurationPropertyCollection properties;
            private static ConfigurationProperty propAssembly;
    
            protected override ConfigurationPropertyCollection Properties
            {
                get
                {
                    return properties;
                }
            }
    
            public PluginsElement()
            {
                propAssembly = new ConfigurationProperty("assembly", typeof(string),
                                                              null,
                                                              ConfigurationPropertyOptions.IsKey);
                properties = new ConfigurationPropertyCollection { propAssembly };
            }
    
            public PluginsElement(string assemblyName)
                : this()
            {
                AssemblyName = assemblyName;
            }
    
            public string AssemblyName
            {
                get
                {
                    return this[propAssembly] as string;
                }
                set
                {
                    this[propAssembly] = value;
                }
            }
        }
    }
    

    And to access it, this code snippet should help:

            var cfg = WebConfigurationManager.GetWebApplicationSection("plugins") as PluginsConfiguration;
            var sb = new StringBuilder();
            foreach(PluginsElement elem in cfg.Plugins)
            {
                sb.AppendFormat("{0}<br/>", elem.AssemblyName);
            }
            testLabel.Text = sb.ToString();
    

    Basically we have a ConfigurationSection that handles the plugins-element. In this, we specify a ConfigurationElementCollection property and declare it as the Default Collection (you could in theory have multiple different collections under one root node).

    PluginsElementCollection implements the ConfigurationElementCollection. ElementName has to be the name of the tag, in our case “use”. Also, GetElementKey needs to be overridden and should return an attribute that is unique among the entries.

    PluginsElement then implements a single use tag. We only define one property: AssemblyName, which is mapped to the assembly-attribute.

    I don’t claim to fully understand all of this (Especially ConfigurationElementCollection and it’s various BaseAdd, BaseGet etc. properties are not really explored here), but I can claim that this works 🙂

    Also, It doesn’t use any attributes. I hate Attributes – luckily, all these attributes can be converted to proper code. You could use one or the other (or both).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 279k
  • Answers 279k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer ObservableCollection is based on Collection which implements IEnumerable. You are… May 13, 2026 at 3:31 pm
  • Editorial Team
    Editorial Team added an answer No such command -- there are no different working modes… May 13, 2026 at 3:31 pm
  • Editorial Team
    Editorial Team added an answer Ahh, the issue is now resolved after stepping already like… May 13, 2026 at 3:31 pm

Related Questions

I would like to accomplish two things during my build process: Run unit tests
I have an existing ASP.NET application that implements Forms Authentication site-wide. The application is
I'm creating an automated installation of Office 2007. To customise your Office 2007 installation
Lets say I have an old application which will try to load an external
I have a controller action that allows a user to download a file with

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.