当前位置:编程学习 > html/css >>

自定义App.config NameValueSectionHandler的bug修复创建自己的NameValueCollectionSectionHandler

在很多时候我们需要自定义我们自己的自定义App.config 文件,而微软为我们提供了默认的

      System.Configuration.NameValueSectionHandler
      System.Configuration.DictionarySectionHandler
      System.Configuration.SingleTagSectionHandler

经常用这些也没有出现问题。今天因项目需求的问题再次使用System.Configuration.NameValueSectionHandler,相应的配置如下:


[html]
<configSections> 
    <section name="mySection1"  type="System.Configuration.NameValueSectionHandler, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"/> 
  </configSections> 
  <mySection1> 
    <add key="filepath" value="Views/Channel/men"></add> 
    <add key="filepath" value="Views/Channel/homme"></add> 
    <add key="filepath" value="Views/Channel/fleece/20120906"></add> 
    <add key="filepath" value="Views/Channel/Designer"></add> 
    <add key="filepath" value="Views/Channel/coats/20120816"></add> 
    <add key="filepath" value="Views/Channel/xiuxianku/20120517"></add> 
    <add key="filepath" value="men"></add> 
    <add key="filepath" value="homme"></add> 
    <add key="filepath" value="fleece"></add> 
    <add key="filepath" value="Designer"></add> 
    <add key="filepath" value="coats"></add> 
    <add key="filepath" value="xiuxianku"></add> 
  </mySection1> 

<configSections>
    <section name="mySection1"  type="System.Configuration.NameValueSectionHandler, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"/>
  </configSections>
  <mySection1>
    <add key="filepath" value="Views/Channel/men"></add>
    <add key="filepath" value="Views/Channel/homme"></add>
    <add key="filepath" value="Views/Channel/fleece/20120906"></add>
    <add key="filepath" value="Views/Channel/Designer"></add>
    <add key="filepath" value="Views/Channel/coats/20120816"></add>
    <add key="filepath" value="Views/Channel/xiuxianku/20120517"></add>
    <add key="filepath" value="men"></add>
    <add key="filepath" value="homme"></add>
    <add key="filepath" value="fleece"></add>
    <add key="filepath" value="Designer"></add>
    <add key="filepath" value="coats"></add>
    <add key="filepath" value="xiuxianku"></add>
  </mySection1>
然后在读取相应的配置信息。
  

[csharp]
NameValueCollection mySection1 = ((NameValueCollection)ConfigurationManager.GetSection("mySection1")); 
   List<string> fileList = mySection1["filepath"].Split(new char[] { ',' }).ToList(); 

         NameValueCollection mySection1 = ((NameValueCollection)ConfigurationManager.GetSection("mySection1"));
            List<string> fileList = mySection1["filepath"].Split(new char[] { ',' }).ToList();得到的结果只有最后一条,而我们所用的NameValueCollection不一致,让我们来看看源码吧:
[csharp]
internal static object CreateStatic(object parent, XmlNode section, string keyAttriuteName, string valueAttributeName) 

    ReadOnlyNameValueCollection values; 
    if (parent == null) 
    { 
        values = new ReadOnlyNameValueCollection(StringComparer.OrdinalIgnoreCase); 
    } 
    else 
    { 
        ReadOnlyNameValueCollection values2 = (ReadOnlyNameValueCollection) parent; 
        values = new ReadOnlyNameValueCollection(values2); 
    } 
    HandlerBase.CheckForUnrecognizedAttributes(section); 
    foreach (XmlNode node in section.ChildNodes) 
    { 
        if (!HandlerBase.IsIgnorableAlsoCheckForNonElement(node)) 
        { 
            if (node.Name == "add") 
            { 
                string str = HandlerBase.RemoveRequiredAttribute(node, keyAttriuteName); 
                string str2 = HandlerBase.RemoveRequiredAttribute(node, valueAttributeName, true); 
                HandlerBase.CheckForUnrecognizedAttributes(node); 
                values[str] = str2; 
            } 
            else if (node.Name == "remove") 
            { 
                string name = HandlerBase.RemoveRequiredAttribute(node, keyAttriuteName); 
                HandlerBase.CheckForUnrecognizedAttributes(node); 
                values.Remove(name); 
            } 
            else if (node.Name.Equals("clear")) 
            { 
                HandlerBase.CheckForUnrecognizedAttributes(node); 
                values.Clear(); 
          &n

补充:web前端 , HTML 5 ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,