300字范文,内容丰富有趣,生活中的好帮手!
300字范文 > 使用ConfigurationManager来写自己的配置文件

使用ConfigurationManager来写自己的配置文件

时间:2022-04-20 20:08:06

相关推荐

使用ConfigurationManager来写自己的配置文件

在.NET 1.1下,你必须用过实现IConfigurationSectionHandler接口来进行操作。

但在.NET 2.0下,微软提供了一系列可以对配置文件进行操作的方法,而且非常强大,能够让你随便定义自己的配置节点。在开始之前我们先看看我们想写一个怎样的配置节:

<configSections>

<sectionname="MailSettings"type="MyBlog.MailSection,MyBlog"/>

</configSections>

这个地方指定了我们想要写一个MailSettings的节,后面的type标识将会使用MyBlog这个assembly理的MyBlog.MailSection类来识别这个section。

<MailSettings>

<MailPlugins>

<addname="Server"value=""/>

<addname="Title"value=""/>

<addname="Body"value=""/>

</MailPlugins>

</MailSettings>

这个地方有是我们自定义的一些配置了,value就是我们所需要的一些值。

现在目标已经有了,下面就是怎么去用这些API来读出我们需要的值:

首先很自然我们需要MailSettings的这个section,于是我们定义类:

publicsealedclassMailSection:ConfigurationSection

{

}

一个ConfigurationSection可以有自己的一些attribute,这些网上资料很多,我就不详解了。我关心的是下面的MailPlugins这层,这层在配置文件上被称为ConfigurationElementCollection,也就是一些ConfigurationElement的集合。那么我们先定义所需要的ConfigurationElement:

publicsealedclassMailPluginElement:ConfigurationElement

{

[ConfigurationProperty("name",IsRequired=true,IsKey=true)]

publicstringName

{

get{return(string)this["name"];}

set{this["name"]=value;}

}

[ConfigurationProperty("value",IsRequired=true)]

publicstringValue

{

get{return(string)this["value"];}

set{this["value"]=value;}

}

}

接着是定义这个ConfigurationElement的集合:

publicsealedclassMailPluginElementCollection:ConfigurationElementCollection

{

protectedoverrideConfigurationElementCreateNewElement()

{

returnnewMailPluginElement();

}

protectedoverrideobjectGetElementKey(ConfigurationElementelement)

{

return((MailPluginElement)element).Name;

}

}

默认的抽象类是让我们必须override上面的两个保护方法。但是这样是不能满足我们的要求的,我们希望用add key 和 value这种形式来配置:

publicoverrideConfigurationElementCollectionTypeCollectionType

{

get

{

returnConfigurationElementCollectionType.AddRemoveClearMap;

}

}

这里相当重要!需要使用AddRemoveClearMap来确保配置处理节能够识别<add key="" value="" />,如果配置为别的会报给你add节无法识别的错误!

那么最后我们一个常用的功能没有提供:索引器!于是我们再加上:

publicMailPluginElementthis[intindex]

{

get{returnBaseGet(index)asMailPluginElement;}

}

publicnewMailPluginElementthis[stringname]

{

get{returnBaseGet(name)asMailPluginElement;}

}

那么ConfigurationElementCollection告一段落了,现在就是把这些东西塞进ConfigurationSection了:

publicsealedclassMailSection:ConfigurationSection

{

[ConfigurationProperty("MailPlugins")]

publicMailPluginElementCollectionMailPlugins

{

get{return(MailPluginElementCollection)base["MailPlugins"];}

}

}

注意这里的ConfigurationProperty这个Attribute,里面的字符串就是你在配置节里的那个element group节点。当然如果你想省事的话你可以定义为"",这样上面的配置文件就变为:

<MailSettings>

<addname="Server"value=""/>

<addname="Title"value=""/>

<addname="Body"value=""/>

</MailSettings>

最后便是如何使用了:

publicstringGetConfigValue(stringname)

{

MailSectionmailSection=System.Configuration.ConfigurationManager.GetSection("MailSettings")asMailSection;

MailPluginElementCollectionmailInfos=mailSection.MailPlugins;

returnmailInfos[name].Value;

}

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。