how to have custom attribute in ConfigurationElementCollection?

后端 未结 4 729
深忆病人
深忆病人 2020-12-23 18:34

for configuration as following


  
  ... other entries
         


        
4条回答
  •  忘掉有多难
    2020-12-23 19:01

    If you want to genericize it, this should help:

    using System.Configuration;
    
    namespace Abcd
    {
      // Generic implementation of ConfigurationElementCollection.
      [ConfigurationCollection(typeof(ConfigurationElement))]
      public class ConfigurationElementCollection : ConfigurationElementCollection
                                             where T : ConfigurationElement, IConfigurationElement, new()
      {
        protected override ConfigurationElement CreateNewElement()
        {
          return new T();
        }
    
        protected override object GetElementKey(ConfigurationElement element)
        {
          return ((IConfigurationElement)element).GetElementKey();
        }
    
        public T this[int index]
        {
          get { return (T)BaseGet(index); }
        }
    
        public T GetElement(object key)
        {
          return (T)BaseGet(key);
        }
      }
    }
    

    Here's the interface referenced above:

    namespace Abcd
    {
      public interface IConfigurationElement
      {
        object GetElementKey();
      }
    }
    

提交回复
热议问题