How to include simple collections in ConfigurationSection

前端 未结 4 596
不思量自难忘°
不思量自难忘° 2020-12-20 11:49

Is there a way for me to include a simple array of strings, or List on my custom subclass of ConfigurationSection? (Or an array or generic list of simple data

4条回答
  •  刺人心
    刺人心 (楼主)
    2020-12-20 12:36

    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:

    
    
      
        

提交回复
热议问题