自定义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();
}
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一样。
上一篇: PhoneGap-----Accelerometer
下一篇: 暑假带孩子去国外哪里旅游好?