zoukankan      html  css  js  c++  java
  • Custom ConfigurationSection

     1 <?xml version="1.0" encoding="utf-8" ?>
     2 <configuration>
     3   <configSections>
     4     <section name="PublishGeneralMessageRQTransformerConfiguration" type="ConsoleApplication5.PublishGeneralMessageRQTransformerEventSection, ConsoleApplication5" />
     5   </configSections>
     6   <PublishGeneralMessageRQTransformerConfiguration>
     7     <eventItems>
     8       <add EventCode="EventA" ActionCode="ActionCodeA" Category="CategoryA" IsRetrieveGUIData="false" />
     9       <add EventCode="EventB" ActionCode="ActionCodeB" Category="CategoryB" IsRetrieveGUIData="true" />
    10       <add EventCode="EventC" ActionCode="ActionCodeC" Category="CategoryC" IsRetrieveGUIData="true" />
    11       <add EventCode="EventD" ActionCode="ActionCodeD" Category="CategoryD" IsRetrieveGUIData="true" />
    12     </eventItems>
    13   </PublishGeneralMessageRQTransformerConfiguration>
    14 </configuration>


      1 using System;
      2 using System.Collections.Generic;
      3 using System.Linq;
      4 using System.Text;
      5 using System.Configuration;
      6 
      7 namespace ConsoleApplication5
      8 {
      9     class Program
     10     {
     11         static void Main(string[] args)
     12         {
     13 //            var element = PublishGeneralMessageRQTransformerConfigHelper.GetElement("EventA");
     14 
     15             foreach (EventConfigElement element in PublishGeneralMessageRQTransformerConfigHelper.GetElements())
     16             {
     17                 Console.WriteLine(element.EventCode);
     18                 Console.WriteLine(element.ActionCode);
     19                 Console.WriteLine(element.Category);
     20                 Console.WriteLine(element.IsRetrieveGUIData);
     21             }
     22 
     23             Console.ReadKey();
     24         }
     25 
     26         public class PublishGeneralMessageRQTransformerConfigHelper
     27         {
     28             public static bool ContainsEvent(string eventCode)
     29             {
     30                 var element = GetElement(eventCode);
     31 
     32                 return element != null;
     33             }
     34 
     35             public static EventConfigElement GetElement(string eventCode)
     36             {
     37                 var elements = GetConfig().EventElements;
     38 
     39                 if (elements != null)
     40                 {
     41                     foreach (EventConfigElement aElement in elements)
     42                     {
     43                         if (aElement.EventCode == eventCode) return aElement;
     44                     }
     45                 }
     46 
     47                 return null;
     48             }
     49 
     50             public static EventConfigElements GetElements()
     51             {
     52                 return GetConfig().EventElements;
     53             }
     54 
     55             private static PublishGeneralMessageRQTransformerEventSection GetConfig()
     56             {
     57                 return ConfigurationManager.GetSection(PublishGeneralMessageRQTransformerEventSection.EventSection) as PublishGeneralMessageRQTransformerEventSection;
     58             }
     59         }
     60     }
     61 
     62     public class PublishGeneralMessageRQTransformerEventSection : ConfigurationSection
     63     {
     64         public const string EventSection = "PublishGeneralMessageRQTransformerConfiguration";
     65         private const string EventConfigElementsKey = "eventItems";
     66 
     67         [ConfigurationProperty(EventConfigElementsKey, IsDefaultCollection = true, IsRequired = true)]
     68         public EventConfigElements EventElements
     69         {
     70             get
     71             {
     72                 return (EventConfigElements)this[EventConfigElementsKey];
     73             }
     74             set
     75             {
     76                 base[EventConfigElementsKey] = value;
     77             }
     78         }
     79     }
     80 
     81     public class EventConfigElements : ConfigurationElementCollection
     82     {
     83         public EventConfigElement this[int index]
     84         {
     85             get
     86             {
     87                 return base.BaseGet(index) as EventConfigElement;
     88             }
     89             set
     90             {
     91                 if (base.BaseGet(index) != null)
     92                 {
     93                     base.BaseRemoveAt(index);
     94                 }
     95                 this.BaseAdd(index, value);
     96             }
     97         }
     98 
     99         protected override ConfigurationElement CreateNewElement()
    100         {
    101             return new EventConfigElement();
    102         }
    103 
    104         protected override object GetElementKey(ConfigurationElement element)
    105         {
    106             return ((EventConfigElement)element).ElementInformation;
    107         }
    108     }
    109 
    110     public class EventConfigElement : ConfigurationElement
    111     {
    112         private const string EventCodeName = "EventCode";
    113         private const string CategoryName = "Category";
    114         private const string ActionCodeName = "ActionCode";
    115         private const string IsRetrieveGUIDataName = "IsRetrieveGUIData";
    116 
    117         [ConfigurationProperty(EventConfigElement.EventCodeName, IsRequired = true)]
    118         public string EventCode
    119         {
    120             get
    121             {
    122                 return (string)this[EventCodeName];
    123             }
    124             set
    125             {
    126                 base[EventCodeName] = value;
    127             }
    128         }
    129 
    130         [ConfigurationProperty(EventConfigElement.CategoryName, IsRequired = true)]
    131         public string Category
    132         {
    133             get
    134             {
    135                 return (string)this[CategoryName];
    136             }
    137             set
    138             {
    139                 base[CategoryName] = value;
    140             }
    141         }
    142 
    143         [ConfigurationProperty(EventConfigElement.ActionCodeName, IsRequired = true)]
    144         public string ActionCode
    145         {
    146             get
    147             {
    148                 return (string)this[ActionCodeName];
    149             }
    150             set
    151             {
    152                 base[ActionCodeName] = value;
    153             }
    154         }
    155 
    156         [ConfigurationProperty(EventConfigElement.IsRetrieveGUIDataName, IsRequired = false)]
    157         public bool IsRetrieveGUIData
    158         {
    159             get
    160             {
    161                 return (bool)this[IsRetrieveGUIDataName];
    162             }
    163             set
    164             {
    165                 base[IsRetrieveGUIDataName] = value;
    166             }
    167         }
    168     }
    169 }


  • 相关阅读:
    C#学习记录(一)
    C#学习记录(五)第一次上机实验
    C#学习记录(四)
    C#学习记录(三)
    统计代码行数
    夜深了
    写在岁末的烟花易冷
    c语言|博客作业02
    c语言I博客作业03
    设计模式之模板方法模式
  • 原文地址:https://www.cnblogs.com/sskset/p/1869029.html
Copyright © 2011-2022 走看看