|
I started writing a configuration very early, but I found that the configuration that comes with net is not clear.
Look at the code below app.config
<? xml version = "1.0" encoding = "utf-8"?>
<configuration>
<configSections>
<section name = "myCustomerSectionA" type = "System.Configuration.NameValueSectionHandler" />
<section name = "myCustomerSectionB" type = "ConfigurationDemoII.CustomerSectionHandlers, ConfigurationDemoII" />
</ configSections>
<myCustomerSectionA>
<add key = "myKeyA" value = "myKeyAValueA" />
<add key = "myKeyA" value = "myKeyAValueB" />
<add key = "myKeyB" value = "myKeyBValueA" />
<add key = "myKeyB" value = "myKeyBValueB" />
</ myCustomerSectionA>
<myCustomerSectionB>
<add key = "myKeyA" value = "myKeyAValueA" />
<add key = "myKeyA" value = "myKeyAValueB" />
<add key = "myKeyB" value = "myKeyBValueA" />
<add key = "myKeyB" value = "myKeyBValueB" />
</ myCustomerSectionB>
</ configuration>
app.cs
using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Text;
using System.Xml;
using System.Configuration;
namespace ConfigurationDemoII
{
class Program
{
static void Main (string [] args)
{
try
{
TestSysNameValueCollection ();
Console.WriteLine ("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~";);
TestMyNameValueCollection ();
}
catch (Exception ex)
{
Console.WriteLine (ex.Message);
Console.WriteLine (ex.StackTrace);
}
}
private static void TestSysNameValueCollection ()
{
NameValueCollection nvc = (NameValueCollection) ConfigurationManager.GetSection ("myCustomerSectionA");
foreach (string key in nvc.AllKeys)
{
Console.WriteLine (key);
Console.WriteLine (nvc [key]);
}
}
private static void TestMyNameValueCollection ()
{
NameValueCollection nvc = (NameValueCollection) ConfigurationManager.GetSection ("myCustomerSectionB");
foreach (string key in nvc.AllKeys)
{
Console.WriteLine (key);
Console.WriteLine (nvc [key]);
}
}
}
class CustomerSectionHandlers: IConfigurationSectionHandler
{
#region IConfigurationSectionHandler Members
object System.Configuration.IConfigurationSectionHandler.Create (object parent, object configContext, System.Xml.XmlNode section)
{
NameValueCollection col = new NameValueCollection ();
foreach (XmlNode childNode in section.ChildNodes)
{
XmlElement root = (XmlElement) childNode;
if (root.Name == "add")
{
string key = root.GetAttribute ("key");
string value = root.GetAttribute ("value");
col.Add (key, value);
}
else if (root.Name == "remove")
{
}
else if (root.Name == "clear")
{
}
}
return col;
}
#endregion
}
}
Results of the run
myKeyA
myKeyAValueB
myKeyB
myKeyBValueB
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
myKeyA
myKeyAValueA, myKeyAValueB
myKeyB
myKeyBValueA, myKeyBValueB
Press any key to continue ...
Can you see any difference? The namevaluecollection that comes with Sys seems to flush out the information of the same key that was added later. If the configuration itself is designed this way, why use a NameValueCollection container?
I did not find relevant information in several articles about configuration in cnblog http://msdn.microsoft.com/msdn-online/shared/components/ratings/ratings.aspx? ContentID = _978498&HideDiscuss = 1 A similar introduction
OK, open Reflect and take a look.
internal static object CreateStatic (object parent, XmlNode section, string keyAttriuteName, string valueAttributeName)
{
ReadOnlyNameValueCollection collection1;
if (parent == null)
{
collection1 = new ReadOnlyNameValueCollection (new CaseInsensitiveHashCodeProvider (CultureInfo.InvariantCulture), new CaseInsensitiveComparer (CultureInfo.InvariantCulture));
}
else
{
ReadOnlyNameValueCollection collection2 = (ReadOnlyNameValueCollection) parent;
collection1 = new ReadOnlyNameValueCollection (collection2);
}
HandlerBase.CheckForUnrecognizedAttributes (section);
foreach (XmlNode node1 in section.ChildNodes)
{
if (! HandlerBase.IsIgnorableAlsoCheckForNonElement (node1))
{
if (node1.Name == "add")
{
string text1 = HandlerBase.RemoveRequiredAttribute (node1, keyAttriuteName);
string text2 = HandlerBase.RemoveRequiredAttribute (node1, valueAttributeName, true);
HandlerBase.CheckForUnrecognizedAttributes (node1);
collection1 [text1] = text2;
}
else
{
if (node1.Name == "remove")
{
string text3 = HandlerBase.RemoveRequiredAttribute (node1, keyAttriuteName);
HandlerBase.CheckForUnrecognizedAttributes (node1);
collection1.Remove (text3);
continue;
}
if (node1.Name.Equals ("clear"))
{HandlerBase.CheckForUnrecognizedAttributes (node1);
collection1.Clear ();
continue;
}
HandlerBase.ThrowUnrecognizedElement (node1);
}
}
}
collection1.SetReadOnly ();
return collection1;
}
ReadOnlyNameValueCollection This thing SDK does not introduce HandlerBase This thing has not been studied in depth, halo
Just be lazy and wait for the cow to answer
The past cattle, please leave a good opinion |
|