首页前端开发HTML自定义App.config NameValueSectionHandler的bug修复创建自己的NameValueCollectionSectionHandler

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

时间2024-01-25 12:28:26发布访客分类HTML浏览842
导读:收集整理的这篇文章主要介绍了html5教程-自定义App.config NameValueSectionHandler的bug修复创建自己的NameValueCollectionSectionHandler,觉得挺不错的,现在分享给大家,也...
收集整理的这篇文章主要介绍了html5教程-自定义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"));  
   Liststring> fileList = mySection1["filepath"].SplIT(new char[] { ',' } ).ToList();  

         NameValueCollection mySection1 = ((NameValueCollection)ConfigurationManager.GetSection("mySection1"));
            Liststring> 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();  
            }  
            else 
            {  
                HandlerBase.ThrowUnrecognizedElement(node);  
            }  
        }  
    }  
    values.SetReadOnly();  
    return values;  
}  

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();
            }
            else
            {
                HandlerBase.ThrowUnrecognizedElement(node);
            }
        }
    }
    values.SetReadOnly();
    return values;
}

很明显在add的时候它用的是values[str] = str2; 而不是调用add方法。

修改后代码:


[csharp]
public class NameValueCollectionSectionHandler : IConfigurationSectionHandler 
  {  
      // Fields  
      PRivate const string defaultKeyAttribute = "key";  
      private const string defaultValueAttribute = "value";  
 
      // Methods  
      public object Create(object parent, object context, XmlNode section) 
      {  
          return CreateStatic(parent, section, this.KeyAttributeName, this.ValueAttributeName);  
      }  
 
      internal static object CreateStatic(object parent, XmlNode section) 
      {  
          return CreateStatic(parent, section, "key", "value");  
      }  
 
      internal static object CreateStatic(object parent, XmlNode section, string keyAttriuteName, string valueAttributeName) 
      {  
          NameValueCollection values;  
          if (parent == null) 
          {  
              values = new NameValueCollection(StringComparer.OrdinalIgnoreCase);  
          }  
          else 
          {  
              NameValueCollection values2 = (NameValueCollection)parent;  
              values = new NameValueCollection(values2);  
          }  
 
          foreach (XmlNode node in section.ChildNodes) 
          {  
              if (node.Name == "add") 
              {  
                  string key = node.Attributes[keyAttriuteName].Value;  
                  string value = node.Attributes[valueAttributeName].Value;  
                  values.Add(key, value);  
              }  
              else if (node.Name == "remove") 
              {  
                  string key = node.Attributes[keyAttriuteName].Value;  
                  values.Remove(key);  
              }  
              else if (node.Name.Equals("clear")) 
              {  
                  values.Clear();  
              }  
          }  
          return values;  
      }  
 
      // Properties  
      protected virtual string KeyAttributeName 
      {  
          get 
          {  
              return "key";  
          }  
      }  
 
      protected virtual string ValueAttributeName 
      {  
          get 
          {  
              return "value";  
          }  
      }  
  }  

  public class NameValueCollectionSectionHandler : IConfigurationSectionHandler
    {
        // Fields
        private const string defaultKeyAttribute = "key";
        private const string defaultValueAttribute = "value";

        // Methods
        public object Create(object parent, object context, XmlNode section)
        {
            return CreateStatic(parent, section, this.KeyAttributeName, this.ValueAttributeName);
        }

        internal static object CreateStatic(object parent, XmlNode section)
        {
            return CreateStatic(parent, section, "key", "value");
        }

        internal static object CreateStatic(object parent, XmlNode section, string keyAttriuteName, string valueAttributeName)
        {
            NameValueCollection values;
            if (parent == null)
            {
                values = new NameValueCollection(StringComparer.OrdinalIgnoreCase);
            }
            else
            {
                NameValueCollection values2 = (NameValueCollection)parent;
                values = new NameValueCollection(values2);
            }

            foreach (XmlNode node in section.ChildNodes)
            {
                if (node.Name == "add")
                {
                    string key = node.Attributes[keyAttriuteName].Value;
                    string value = node.Attributes[valueAttributeName].Value;
                    values.Add(key, value);
                }
                else if (node.Name == "remove")
                {
                    string key = node.Attributes[keyAttriuteName].Value;
                    values.Remove(key);
                }
                else if (node.Name.Equals("clear"))
                {
                    values.Clear();
                }
            }
            return values;
        }

        // Properties
        protected virtual string KeyAttributeName
        {
            get
            {
                return "key";
            }
        }

        protected virtual string ValueAttributeName
        {
            get
            {
                return "value";
            }
        }
    }
当然这样运行结果就和我们常用的NameValueCollection一样。

 

 

 

在很多时候我们需要自定义我们自己的自定义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"));  
   Liststring> fileList = mySection1["filepath"].Split(new char[] { ',' } ).ToList();  

         NameValueCollection mySection1 = ((NameValueCollection)ConfigurationManager.GetSection("mySection1"));
            Liststring> 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();  
            }  
            else 
            {  
                HandlerBase.ThrowUnrecognizedElement(node);  
            }  
        }  
    }  
    values.SetReadOnly();  
    return values;  
}  

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();
            }
            else
            {
                HandlerBase.ThrowUnrecognizedElement(node);
            }
        }
    }
    values.SetReadOnly();
    return values;
}

很明显在add的时候它用的是values[str] = str2; 而不是调用add方法。

修改后代码:


[csharp]
public class NameValueCollectionSectionHandler : IConfigurationSectionHandler 
  {  
      // Fields  
      private const string defaultKeyAttribute = "key";  
      private const string defaultValueAttribute = "value";  
 
      // Methods  
      public object Create(object parent, object context, XmlNode section) 
      {  
          return CreateStatic(parent, section, this.KeyAttributeName, this.ValueAttributeName);  
      }  
 
      internal static object CreateStatic(object parent, XmlNode section) 
      {  
          return CreateStatic(parent, section, "key", "value");  
      }  
 
      internal static object CreateStatic(object parent, XmlNode section, string keyAttriuteName, string valueAttributeName) 
      {  
          NameValueCollection values;  
          if (parent == null) 
          {  
              values = new NameValueCollection(StringComparer.OrdinalIgnoreCase);  
          }  
          else 
          {  
              NameValueCollection values2 = (NameValueCollection)parent;  
              values = new NameValueCollection(values2);  
          }  
 
          foreach (XmlNode node in section.ChildNodes) 
          {  
              if (node.Name == "add") 
              {  
                  string key = node.Attributes[keyAttriuteName].Value;  
                  string value = node.Attributes[valueAttributeName].Value;  
                  values.Add(key, value);  
              }  
              else if (node.Name == "remove") 
              {  
                  string key = node.Attributes[keyAttriuteName].Value;  
                  values.Remove(key);  
              }  
              else if (node.Name.Equals("clear")) 
              {  
                  values.Clear();  
              }  
          }  
          return values;  
      }  
 
      // Properties  
      protected virtual string KeyAttributeName 
      {  
          get 
          {  
              return "key";  
          }  
      }  
 
      protected virtual string ValueAttributeName 
      {  
          get 
          {  
              return "value";  
          }  
      }  
  }  

  public class NameValueCollectionSectionHandler : IConfigurationSectionHandler
    {
        // Fields
        private const string defaultKeyAttribute = "key";
        private const string defaultValueAttribute = "value";

        // Methods
        public object Create(object parent, object context, XmlNode section)
        {
            return CreateStatic(parent, section, this.KeyAttributeName, this.ValueAttributeName);
        }

        internal static object CreateStatic(object parent, XmlNode section)
        {
            return CreateStatic(parent, section, "key", "value");
        }

        internal static object CreateStatic(object parent, XmlNode section, string keyAttriuteName, string valueAttributeName)
        {
            NameValueCollection values;
            if (parent == null)
            {
                values = new NameValueCollection(StringComparer.OrdinalIgnoreCase);
            }
            else
            {
                NameValueCollection values2 = (NameValueCollection)parent;
                values = new NameValueCollection(values2);
            }

            foreach (XmlNode node in section.ChildNodes)
            {
                if (node.Name == "add")
                {
                    string key = node.Attributes[keyAttriuteName].Value;
                    string value = node.Attributes[valueAttributeName].Value;
                    values.Add(key, value);
                }
                else if (node.Name == "remove")
                {
                    string key = node.Attributes[keyAttriuteName].Value;
                    values.Remove(key);
                }
                else if (node.Name.Equals("clear"))
                {
                    values.Clear();
                }
            }
            return values;
        }

        // Properties
        protected virtual string KeyAttributeName
        {
            get
            {
                return "key";
            }
        }

        protected virtual string ValueAttributeName
        {
            get
            {
                return "value";
            }
        }
    }
当然这样运行结果就和我们常用的NameValueCollection一样。

 

 

 

觉得可用,就经常来吧! 欢迎评论哦! html5教程,巧夺天工,精雕玉琢。小宝典献丑了!

声明:本文内容由网友自发贡献,本站不承担相应法律责任。对本内容有异议或投诉,请联系2913721942#qq.com核实处理,我们将尽快回复您,谢谢合作!

ClassconstdivHTMLletpost-format-galleryPropthis

若转载请注明出处: 自定义App.config NameValueSectionHandler的bug修复创建自己的NameValueCollectionSectionHandler
本文地址: https://pptw.com/jishu/586514.html
boost::xml 用纯css实现的html5 logo标志

游客 回复需填写必要信息