zoukankan      html  css  js  c++  java
  • 自定义配置节备忘

    View Code
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Configuration;
    
    namespace ClassGenerator.ConfigSection
    {
        public class MyKeyValuePair : ConfigurationElement
        {
            public MyKeyValuePair() { }
    
            public MyKeyValuePair(string key, string value)
            {
                this.Key = key;
                this.Value = value;
            }
    
            [ConfigurationProperty("key", DefaultValue = "", IsRequired = true, IsKey = true)]
            public string Key
            {
                get
                {
                    return this["key"] as string;
                }
                set
                {
                    this["key"] = value;
                }
            }
    
            [ConfigurationProperty("value", DefaultValue = "", IsRequired = true)]
            public string Value
            {
                get
                {
                    return this["value"] as string;
                }
                set
                {
                    this["value"] = value;
                }
            }
        }
    }
    View Code
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Configuration;
    
    namespace ClassGenerator.ConfigSection
    {
        public class MyKeyValuePairCollection : ConfigurationElementCollection
        {
            protected override ConfigurationElement CreateNewElement()
            {
                return new MyKeyValuePair();
            }
    
            protected override object GetElementKey(ConfigurationElement element)
            {
                return (element as MyKeyValuePair).Key.ToUpper();
            }
    
            public void Add(MyKeyValuePair element)
            {
                base.BaseAdd(element);
            }
    
            public void Remove(string key)
            {
                base.BaseRemove(key.ToUpper());
            }
    
            public new MyKeyValuePair this[string key]
            {
                get
                {
                    return base.BaseGet(key) as MyKeyValuePair;
                }
            }
        }
    }
    View Code
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Configuration;
    
    namespace ClassGenerator.ConfigSection
    {
        public class MyKeyValuePairSection : ConfigurationSection
        {
            private const string connectionStringsName = "connectionStrings";
            private const string mappingTypesName = "mappingTypes";
            private const string templatesName = "templates";
    
            public MyKeyValuePairSection() 
            {
                    
            }
    
            [ConfigurationProperty(connectionStringsName, IsDefaultCollection = false)]
            [ConfigurationCollection(typeof(MyKeyValuePairCollection),
                AddItemName = "add",
                ClearItemsName = "clear",
                RemoveItemName = "remove")]
            public MyKeyValuePairCollection Connections
            {
                get
                {
                    return base[connectionStringsName] as MyKeyValuePairCollection;
                }
            }
    
            [ConfigurationProperty(mappingTypesName, IsDefaultCollection = false)]
            [ConfigurationCollection(typeof(MyKeyValuePairCollection),
                AddItemName = "add",
                ClearItemsName = "clear",
                RemoveItemName = "remove")]
            public MyKeyValuePairCollection MappingTypes
            {
                get
                {
                    return base[mappingTypesName] as MyKeyValuePairCollection;
                }
            }
    
            [ConfigurationProperty(templatesName, IsDefaultCollection = false)]
            [ConfigurationCollection(typeof(MyKeyTextPairCollection),
                AddItemName = "add",
                ClearItemsName = "clear",
                RemoveItemName = "remove")]
            public MyKeyTextPairCollection Templates
            {
                get
                {
                    return base[templatesName] as MyKeyTextPairCollection;
                }
            }
        }
    }
    View Code
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Configuration;
    
    namespace ClassGenerator.ConfigSection
    {
        public class MyKeyTextPair : ConfigurationElement
        {
            public MyKeyTextPair() { }
    
            public MyKeyTextPair(string key, string text)
            {
                this.Key = key;
                this.Text = text;
            }
    
            protected override void DeserializeElement(System.Xml.XmlReader reader, bool serializeCollectionKey)
            {
                Key = reader.GetAttribute("key");
                Text = reader.ReadElementContentAs(typeof(string), null) as string;
            }
    
            protected override bool SerializeElement(System.Xml.XmlWriter writer, bool serializeCollectionKey)
            {
                if (writer != null)
                {
                    writer.WriteAttributeString("key", Key);
                    writer.WriteCData(Text);
                }
                return true;
            }
    
            [ConfigurationProperty("key", DefaultValue = "", IsRequired = true, IsKey = true)]
            public string Key
            {
                get
                {
                    return this["key"] as string;
                }
                set
                {
                    this["key"] = value;
                }
            }
    
            [ConfigurationProperty("text",IsRequired = false)]
            public string Text
            {
                get
                {
                    return this["text"] as string;
                }
                set
                {
                    this["text"] = value;
                }
            }
        }
    }
    View Code
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Configuration;
    
    namespace ClassGenerator.ConfigSection
    {
        public class MyKeyTextPairCollection : ConfigurationElementCollection
        {
            protected override ConfigurationElement CreateNewElement()
            {
                return new MyKeyTextPair();
            }
    
            protected override object GetElementKey(ConfigurationElement element)
            {
                return (element as MyKeyTextPair).Key.ToUpper();
            }
    
            public void Add(MyKeyTextPair element)
            {
                base.BaseAdd(element);
            }
    
            public void Remove(string key)
            {
                base.BaseRemove(key.ToUpper());
            }
    
            public new MyKeyTextPair this[string key]
            {
                get
                {
                    return base.BaseGet(key) as MyKeyTextPair;
                }
            }
        }
    }
    <?xml version="1.0" encoding="utf-8" ?>
    <configuration>
      <configSections>
        <section name="MySection" type="ClassGenerator.ConfigSection.MyKeyValuePairSection,ClassGenerator"/>
      </configSections>
      <appSettings />
      <configProtectedData />
      <connectionStrings />
      <system.diagnostics />
      <system.windows.forms />
      <uri />
      <MySection>
        <connectionStrings>
          <add key="a" value="aaaaaa" />
          <add key="b" value="bbbbbb" />
        </connectionStrings>
        <mappingTypes>
          <add key="int" value="Int32" />
          <add key="char" value="String" />
          <add key="varchar" value="String" />
          <add key="nvarchar" value="String" />
          <add key="bit" value="Boolean" />
          <add key="decimal" value="Decimal" />
          <add key="datetime" value="DateTime" />
          <add key="bigint" value="Int64" />
        </mappingTypes>
        <templates>
          <add key="abc">
            <![CDATA[
            
            
            bbb
          
          test
          test]]>
          </add>
          <add key="dce">
            <![CDATA[
            bbbtest
          test]]>
          </add>
          <add key="aaatest">
            <![CDATA[bbbtest]]>
          </add>
        </templates>
      </MySection>
  • 相关阅读:
    HP惠普战66电源黄灯闪烁无法充电
    C#.NET rabbit mq 持久化时报错 durable
    手动解压安装mysql8.0 on windows my.ini
    C#.NET MySql8.0 EF db first
    EF MYSQL 出现:输入字符串的格式不正确
    EF MYSQL DB FIRST 出现2次数据库名
    mysql windows 下配置可远程连接
    团队项目的Git分支管理规范
    一个简单的软件测试流程
    微服务架构下的质量迷思——混沌工程
  • 原文地址:https://www.cnblogs.com/nanfei/p/2515148.html
Copyright © 2011-2022 走看看