实现IConfigurationSectionHandler来获取自定义配置结构

一 创建类AddinSectionHandler ,实现IConfigurationSectionHandler接口的Create方法
ExpandedBlockStart.gif ContractedBlock.gif     public    class  AddinSectionHandler : IConfigurationSectionHandler  dot.gif {
ContractedSubBlock.gifExpandedSubBlockStart.gif       
IConfigurationSectionHandler 成员#region IConfigurationSectionHandler 成员
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif       
public object Create(object parent, object configContext, System.Xml.XmlNode section) dot.gif{
InBlock.gif           List
<AddinConfigItem> list = new List<AddinConfigItem>();
ExpandedSubBlockStart.gifContractedSubBlock.gif           
if (parent != nulldot.gif{
InBlock.gif               list 
= (List<AddinConfigItem>)parent;
ExpandedSubBlockEnd.gif           }

ExpandedSubBlockStart.gifContractedSubBlock.gif           
foreach (XmlNode node in section.ChildNodes) dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif               
if (null != node.Attributes) dot.gif{
InBlock.gif                   AddinConfigItem item 
= new AddinConfigItem();
InBlock.gif                   item.DllFile 
= node.Attributes["dllFile"].Value;
InBlock.gif                   item.AddInClass 
= node.Attributes["addinClass"].Value;
InBlock.gif                   list.Add(item);
ExpandedSubBlockEnd.gif               }

ExpandedSubBlockEnd.gif           }

InBlock.gif           
return list;
ExpandedSubBlockEnd.gif       }

InBlock.gif
ExpandedSubBlockEnd.gif       
#endregion

ExpandedBlockEnd.gif   }

二 增加一个这样的类

ExpandedBlockStart.gif ContractedBlock.gif    public   class  AddinConfigItem  dot.gif {
InBlock.gif
InBlock.gif        
private string _addInClass;
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 添加服务类名称
ExpandedSubBlockEnd.gif        
/// </summary>

ExpandedSubBlockStart.gifContractedSubBlock.gif        public string AddInClass dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
get dot.gifreturn _addInClass; }
ExpandedSubBlockStart.gifContractedSubBlock.gif            
set dot.gif{ _addInClass = value; }
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
private string _dllFile;
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 添加服务数据集名称
ExpandedSubBlockEnd.gif        
/// </summary>

ExpandedSubBlockStart.gifContractedSubBlock.gif        public string DllFile dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
get dot.gifreturn _dllFile; }
ExpandedSubBlockStart.gifContractedSubBlock.gif            
set dot.gif{ _dllFile = value; }
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedBlockEnd.gif    }


三 App.config中配置
<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <configSections>
    <section name="ServerAssemblys" type="ConfigLoad.AddinSectionHandler,ConfigLoad"/>
  </configSections>
  <ServerAssemblys>
    <Assembly dllFile="111.dll" addinClass="aaa" />
    <Assembly dllFile="222.dll" addinClass="bbb" />
  </ServerAssemblys>
</configuration>


四 程序调用
ExpandedBlockStart.gif ContractedBlock.gif   class  Program  dot.gif {
ExpandedSubBlockStart.gifContractedSubBlock.gif        
static void Main(string[] args) dot.gif{
InBlock.gif          
InBlock.gif            
//读取XML中服务节点的内容
InBlock.gif
            List<AddinConfigItem> lists = ConfigurationManager.GetSection("ServerAssemblys"as List<AddinConfigItem>;
InBlock.gif            
//加裁所有服务 
ExpandedSubBlockStart.gifContractedSubBlock.gif
            foreach (AddinConfigItem item in lists) dot.gif{
InBlock.gif
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

ExpandedBlockEnd.gif    }

源码

转载于:https://www.cnblogs.com/qiba78/archive/2008/06/23/1228540.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值