|
很早的时候就开始写一个configuration,但是发现net自带的configuration有些地方让人搞不明白
看下面的代码 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 成员
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
}
}
运行的结果
myKeyA
myKeyAValueB
myKeyB
myKeyBValueB
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
myKeyA
myKeyAValueA,myKeyAValueB
myKeyB
myKeyBValueA,myKeyBValueB
Press any key to continue . . .
能看到什么不同么, Sys自带的namevaluecollection好像把后面新加入的同一个key的信息冲掉了,如果是configuration本身就是这么设计的,那么为何要用NameValueCollection这个容器呢?
我在cnblog的几篇介绍configuration的文章中并没有找到相关的信息 http://msdn.microsoft.com/msdn-online/shared/components/ratings/ratings.aspx?ContentID=_978498&HideDiscuss=1里面也没有这一块类似的介绍
OK, 打开Refelect看看吧 哦,
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 这个东西sdk里面没有介绍 HandlerBase 这个玩意也没有深入研究过,晕
还是偷懒一下,等着牛人来回答吧
过往的牛人, 请留下高见 |
|