Custom ConfigurationSection

本文介绍了一个使用.NET框架中ConfigurationSection进行配置文件管理的具体案例。通过定义自定义配置节,可以方便地在应用程序中读取配置信息,并实现对特定配置元素的操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 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  }


 

 

 

posted on 2010-11-04 14:42 K3 阅读( ...) 评论( ...) 编辑 收藏

转载于:https://www.cnblogs.com/sskset/archive/2010/11/04/1869029.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值