由网友(帅到炸了)分享简介:什么是拿起通过一个类继承自System.Configuration.Section如果我用这样的的app.config页面列表中选择正确的方法是什么?< XML版本=1.0编码=UTF-8&GT?;<结构>< configSections><节名称=XrbSettingsTYPE...![App.config配置文件的连接字符串添加技巧](https://p.xsw88.cn/allimgs/daicuo/20230902/575.png)
什么是拿起通过一个类继承自System.Configuration.Section如果我用这样的的app.config页面列表中选择正确的方法是什么?
< XML版本=1.0编码=UTF-8&GT?;
<结构>
< configSections>
<节名称=XrbSettingsTYPE =Xrb.UI.XrbSettings,Xrb.UI/>
< / configSections>
< XrbSettings>
<网页>
<添加标题=谷歌URL =http://www.google.com/>
<添加标题=雅虎URL =http://www.yahoo.com/>
< /页>
< / XrbSettings>
< /结构>
解决方案
首先,你在扩展部分类添加属性:
[的ConfigurationProperty(页,IsDefaultCollection = FALSE)]
[ConfigurationCollection(typeof运算(PageCollection),AddItemName =加)
公共PageCollection页面{
得到 {
返程(PageCollection)本[页];
}
}
然后,你需要做一个PageCollection类。所有我见过的例子是pretty的很多相同的,从而只是复制这个并重新命名NamedService到页面。
最后补充扩展ObjectConfigurationElement类:
公共类PageElement:ObjectConfigurationElement {
[的ConfigurationProperty(标题,IsRequired =真)
公共字符串名称{
得到 {
返程(串)本[标题];
}
组 {
这种[标题] =价值;
}
}
[的ConfigurationProperty(URL,IsRequired =真)
公共字符串URL {
得到 {
返程(串)本[网址];
}
组 {
这种[网址] =值;
}
}
}
![App.config配置文件的连接字符串添加技巧](https://p.xsw88.cn/allimgs/daicuo/20230902/575.png)
下面是一些文件从一个简单的实现:
示例配置 收集和元素类 配置部分类What is the correct way to pick up the list of "pages" via a class that inherits from System.Configuration.Section if I used a app.config like this?
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="XrbSettings" type="Xrb.UI.XrbSettings,Xrb.UI" />
</configSections>
<XrbSettings>
<pages>
<add title="Google" url="http://www.google.com" />
<add title="Yahoo" url="http://www.yahoo.com" />
</pages>
</XrbSettings>
</configuration>
解决方案
First you add a property in the class that extends Section:
[ConfigurationProperty("pages", IsDefaultCollection = false)]
[ConfigurationCollection(typeof(PageCollection), AddItemName = "add")]
public PageCollection Pages {
get {
return (PageCollection) this["pages"];
}
}
Then you need to make a PageCollection class. All the examples I've seen are pretty much identical so just copy this one and rename "NamedService" to "Page".
Finally add a class that extends ObjectConfigurationElement:
public class PageElement : ObjectConfigurationElement {
[ConfigurationProperty("title", IsRequired = true)]
public string Title {
get {
return (string) this["title"];
}
set {
this["title"] = value;
}
}
[ConfigurationProperty("url", IsRequired = true)]
public string Url {
get {
return (string) this["url"];
}
set {
this["url"] = value;
}
}
}
Here are some files from a sample implementation:
Sample config Collection and element classes Config section class相关推荐
最新文章