实际一个多态(或弱类型)ConfigurationElementCollection

不知道你们有没有过这样的一个需求:

     假如我要实现一个像Windows的任务计划一样的工具。我支持按天执行,或按月执行,甚至我还直接按每个星期几来执行。好吧,做这样一个东西不难。现在我希望把这些运行信息存在配置文件中。那么配置文件可能是这样的结构

  

  <scheduleServiceSection>
    <schedules>
      <add name ="DaySchedule" 
           days="1" startDateTime="2012/11/30 11:59:59" endDateTime="2013/11/30 11:59:59" runTime="11:59:59" >
      </add>
    

      <add name ="MonthSchedule" 
     months="1" runMonths="1,2,3,4,5,6,7,8,9,10,11,12"
           runDays="1,2,3,4,5"
           startDateTime="2012/11/30 11:59:59" endDateTime="2013/11/30 11:59:59" runTime="11:59:59" >
      </add>

      <add name ="WeekSchedule"
     weeks="1" runWeekDays="0,1,2,3,4,5,6"
     startDateTime="2012/11/30 11:59:59" endDateTime="2013/11/30 11:59:59" runTime="11:59:59" >

      </add>
    </schedules>
  </scheduleServiceSection>

 .net 自带的配置集合 ConfigurationElementCollection,只能包含一种类型的ConfigurationElement。不能实现像上面那种WeekSchedule比DaySchedule多一些属性或少一些属性。

解决办法:

https://github.com/Yunanw/CSSnippet/tree/master/PolyConfigCollection

posted on 2012-12-11 11:32  Yunanw  阅读(452)  评论(0编辑  收藏  举报