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

一 创建类AddinSectionHandler ,实现IConfigurationSectionHandler接口的Create方法
   public  class AddinSectionHandler : IConfigurationSectionHandler {
       
IConfigurationSectionHandler 成员
   }

二 增加一个这样的类

  public class AddinConfigItem {

        
private string _addInClass;
        
/// <summary>
        
/// 添加服务类名称
        
/// </summary>

        public string AddInClass {
            
get return _addInClass; }
            
set { _addInClass = value; }
        }


        
private string _dllFile;
        
/// <summary>
        
/// 添加服务数据集名称
        
/// </summary>

        public string DllFile {
            
get return _dllFile; }
            
set { _dllFile = value; }
        }


    }


三 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>


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

            }

        }

    }

源码
posted @ 2008-06-23 23:05  往事随锋  阅读(891)  评论(3编辑  收藏  举报