Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
749 views
in Technique[技术] by (71.8m points)

.net - How to include simple collections in ConfigurationSection

Is there a way for me to include a simple array of strings, or List<string> on my custom subclass of ConfigurationSection? (Or an array or generic list of simple data objects, for that matter?)

I'm becoming familiar with the new (and VERY verbose) ConfigurationSection, ConfigurationElement, and ConfigurationElementCollection classes, but I'm by no means an expert yet.

It seems like ConfigurationSection should handle simple collections/lists on its own, without me having to create a custom ConfigurationElementCollection subclass for each and every one. But I haven't found any reference to this ability online.

Edit: accepting Dan's response as the answer, since it's probably the closest thing I'm going to get to the "old style" configSections. I always found it easy, flexible, and elegant that any XmlSerializable object could easily become a configSection. I'm sure the new framework is more powerful; however it's sad that it is SO cumbersome for simple configuration contructs, that we're reduced to going back to String.Split().

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Here's a simple example.

//START CODE


//MyCompany.MyProject.csproj which results in MyCompany.MyProject.dll
//Add a Folder called "Configuration"

namespace MyCompany.MyProject.Configuration
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;

    using System.Configuration;


    public class TransformationToDirectoryMapping : ConfigurationElement
    {

        private const string FRIENDLY_NAME = "FriendlyName";
        private const string PICKUP_FOLDER = "PickupFolder";

        [ConfigurationProperty(FRIENDLY_NAME, DefaultValue = "", IsKey = false, IsRequired = true)]
        public string FriendlyName
        {
            get
            {
                return ((string)(base[FRIENDLY_NAME]));
            }
            set
            {
                base[FRIENDLY_NAME] = value;
            }
        }

        [ConfigurationProperty(PICKUP_FOLDER, DefaultValue = "", IsKey = true, IsRequired = true)]
        public string PickupFolder
        {
            get
            {
                return ((string)(base[PICKUP_FOLDER]));
            }
            set
            {
                base[PICKUP_FOLDER] = value;
            }
        }



    }

    //-----------------------------------------------------------------------

    //-----------------------------------------------------------------------

    [ConfigurationCollection(typeof(TransformationToDirectoryMapping))]
    public class TransformationToDirectoryMappingCollection : ConfigurationElementCollection
    {

        protected override ConfigurationElement CreateNewElement()
        {
            return new TransformationToDirectoryMapping();
        }

        protected override object GetElementKey(ConfigurationElement element)
        {
            return ((TransformationToDirectoryMapping)(element)).PickupFolder;
        }


        public TransformationToDirectoryMapping this[int idx]
        {
            get
            {
                return (TransformationToDirectoryMapping)BaseGet(idx);
            }
        }

        new public TransformationToDirectoryMapping this[string key]
        {
            get
            {
                return (TransformationToDirectoryMapping)BaseGet(key);
            }
        }
    }

    //-----------------------------------------------------------------------

    //-----------------------------------------------------------------------

    public class TransformationToDirectoryMappingConfigSection : ConfigurationSection
    {
        private const string TRANSFORMATION_TO_DIRECTORY_MAPPINGS = "TransformationToDirectoryMappings";

        [ConfigurationProperty(TRANSFORMATION_TO_DIRECTORY_MAPPINGS)]
        public TransformationToDirectoryMappingCollection TransformationToDirectoryMappingItems
        {
            get { return ((TransformationToDirectoryMappingCollection)(base[TRANSFORMATION_TO_DIRECTORY_MAPPINGS])); }
        }
    }

    //-----------------------------------------------------------------------

    //-----------------------------------------------------------------------

    public static class MyRetriever
    {
        public const string MAPPINGS_CONFIGURATION_SECTION_NAME = "TransformationToDirectoryMappingsSection";

        public static TransformationToDirectoryMappingCollection GetTheCollection()
        {
            TransformationToDirectoryMappingConfigSection mappingsSection = (TransformationToDirectoryMappingConfigSection)ConfigurationManager.GetSection(MAPPINGS_CONFIGURATION_SECTION_NAME);
            if (mappingsSection != null)
            {
                return mappingsSection.TransformationToDirectoryMappingItems;
            }
            return null; // OOPS!

        }
    }

}

//XML for config file:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <configSections>
    <section name="TransformationToDirectoryMappingsSection" type="MyCompany.MyProject.Configuration.TransformationToDirectoryMappingConfigSection, MyCompany.MyProject"/>
  </configSections>

  <TransformationToDirectoryMappingsSection>
    <TransformationToDirectoryMappings>
      <add FriendlyName="Hello" PickupFolder="C:WUWUTemppickupspickup11" />
      <add FriendlyName="GoodBye" PickupFolder="C:WUWUTemppickupspickup12" />
    </TransformationToDirectoryMappings>
  </TransformationToDirectoryMappingsSection>
</configuration>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...