自定义配置的app.config - 部分藏品? [英] Custom Configuration for app.config - collections of sections?
问题描述
我的头是绝对冲击严重!我以前做过这一点,但不作为深度或复杂的,因为这和我曾尝试不同的方式来做到这一点,但都失败了。
所以,这是自定义的XML我想在App.config:
<型材> <! - 收集 - >
<简介姓名=直播与GT;
<成分>
<成分名称=门户TYPE =网/>
<成分名称=通讯科类型=网/>
<成分名称=调度器类型=WindowsService服务名称=LiveScheduler/>
< /成分>
<数据库>
<数据库名称=主连接=数据源= \ SQL2008/>
<数据库名称=临时连接=数据源= SomeSite.co.uk/>
< /数据库>
< /简介>
<档案名称=测试>
<成分>
<成分名称=门户TYPE =网/>
<成分名称=通讯科类型=网/>
<成分名称=调度器类型=WindowsService服务名称=TestScheduler/>
< /成分>
<数据库>
<数据库名称=主连接=数据源= \ SQL2008/>
<数据库名称=临时连接=数据源=内部/>
< /数据库>
< /简介>
< /简介>
所以资料与具有子元素的集合(组件是一个集合,那么组件是一个元素)每个配置集合
不过,我现在都认为除了多个配置文件。那种我确实看到了问题,但不知道如何修理了。
code:
公共类资料:配置节
{
[的ConfigurationProperty(名称,IsRequired =真)
公共字符串名称
{
得到
{
返回基地[名称]作为串;
}
组
{
基地[名称] =值;
}
}
[的ConfigurationProperty(组件)
公共ComponentCollection组件
{
{返回((ComponentCollection)(基地[组件])); }
}
[的ConfigurationProperty(数据库)
公共DatabaseCollection数据库
{
得到
{
返程((DatabaseCollection)(基地[数据库]));
}
}
}
[ConfigurationCollection(typeof运算(组件),AddItemName =分量)]
公共类ComponentCollection:ConfigurationElementCollection
{
保护覆盖的ConfigurationElement CreateNewElement()
{
返回新的组件();
}
保护覆盖对象GetElementKey(的ConfigurationElement元)
{
返回((组件)(元))名称。
}
公共组件本[INT IDX]
{
得到
{
返回base.BaseGet(IDX)作为组分;
}
组
{
如果(base.BaseGet(IDX)!= NULL)
{
base.BaseRemoveAt(IDX);
}
this.BaseAdd(IDX,价值);
}
}
公共组件此[字符串键]
{
得到
{
返回base.BaseGet(密钥)的组件;
}
组
{
如果(base.BaseGet(键)!= NULL)
{
base.BaseRemove(键);
}
this.BaseAdd(this.Count,价值);
}
}
}
公共类组件:的ConfigurationElement
{
[的ConfigurationProperty(类型,IsRequired =真)
公共字符串类型
{
得到
{
返回此[类型]作为串;
}
组
{
这种[类型] =价值;
}
}
[的ConfigurationProperty(名称,IsRequired = TRUE,IsKey =真)
公共字符串名称
{
得到
{
返回此[名称]作为串;
}
组
{
这种[名称] =值;
}
}
[的ConfigurationProperty(服务名称,IsRequired = FALSE)]
公共字符串服务名称
{
得到
{
返回此[服务名称]作为串;
}
组
{
这种[服务名称] =价值;
}
}
}
[ConfigurationCollection(typeof运算(数据库),AddItemName =数据库)
公共类DatabaseCollection:ConfigurationElementCollection
{
保护覆盖的ConfigurationElement CreateNewElement()
{
返回新的数据库();
}
保护覆盖对象GetElementKey(的ConfigurationElement元)
{
。收益率((数据库)(元))名称;
}
公共数据库这一[INT IDX]
{
得到
{
返回base.BaseGet(IDX)数据库;
}
组
{
如果(base.BaseGet(IDX)!= NULL)
{
base.BaseRemoveAt(IDX);
}
this.BaseAdd(IDX,价值);
}
}
公共数据库此[字符串键]
{
得到
{
返回base.BaseGet(密钥)的数据库;
}
组
{
如果(base.BaseGet(键)!= NULL)
{
base.BaseRemove(键);;
}
this.BaseAdd(this.Count,价值);
}
}
}
公共类数据库:的ConfigurationElement
{
[的ConfigurationProperty(名称,IsKey = TRUE,IsRequired =真)
公共字符串名称
{
得到
{
返回此[名称]作为串;
}
组
{
这种[名称] =值;
}
}
[的ConfigurationProperty(连接,IsKey =假,IsRequired =真)
公共字符串连接
{
得到
{
返回此[连接]作为串;
}
组
{
这种[连接] =价值;
}
}
}
您需要将您的配置部分高一个级别。
公共类配置文件:配置节
{
[的ConfigurationProperty(简介)
公共ProfileCollection简介
{
得到
{
返回此[档案]作为ProfileCollection;
}
}
}
下面是我创建了一个部分。你应该能够得到你的工作通过以下方法:
公共类ImportConfiguration:配置节
{
[的ConfigurationProperty(importMap)]
公共ImportMapElementCollection ImportMap
{
得到
{
返回此[importMap]作为ImportMapElementCollection;
}
}
}
公共类ImportColumnMapElement:的ConfigurationElement
{
[的ConfigurationProperty(的localName,IsRequired = TRUE,IsKey =真)
公共字符串的localName
{
得到
{
返回此[的localName]作为串;
}
组
{
这种[的localName] =值;
}
}
[的ConfigurationProperty(SOURCENAME,IsRequired =真)
公共字符串SOURCENAME
{
得到
{
返回此[SOURCENAME]作为串;
}
组
{
这种[SOURCENAME] =值;
}
}
}
公共类ImportMapElementCollection:ConfigurationElementCollection
{
公共ImportColumnMapElement该[对象键]
{
得到
{
返回base.BaseGet(键)为ImportColumnMapElement;
}
}
公众覆盖ConfigurationElementCollectionType CollectionType
{
得到
{
返回ConfigurationElementCollectionType.BasicMap;
}
}
保护覆盖字符串的ElementName
{
得到
{
返回columnMap;
}
}
布尔IsElementName保护覆盖(字符串的ElementName)
{
布尔isName = FALSE;
如果(!String.IsNullOrEmpty(的ElementName))
isName = elementName.Equals(columnMap);
返回isName;
}
保护覆盖的ConfigurationElement CreateNewElement()
{
返回新ImportColumnMapElement();
}
保护覆盖对象GetElementKey(的ConfigurationElement元)
{
返回((ImportColumnMapElement)元).LocalName;
}
}
My head is absolutely pounding badly! I have done this before but not as "in depth" or complex as this and I have tried different ways to make this happen but all has failed.
So, this is the custom XML I want in the app.config:
<Profiles> <!--Collection-->
<Profile Name="Live">
<Components>
<Component Name="Portal" Type="Web" />
<Component Name="Comms" Type="Web" />
<Component Name="Scheduler" Type="WindowsService" ServiceName="LiveScheduler" />
</Components>
<Databases>
<Database Name="Main" Connection="Data Source=.\SQL2008" />
<Database Name="Staging" Connection="Data Source=SomeSite.co.uk" />
</Databases>
</Profile>
<Profile Name="Test">
<Components>
<Component Name="Portal" Type="Web" />
<Component Name="Comms" Type="Web" />
<Component Name="Scheduler" Type="WindowsService" ServiceName="TestScheduler" />
</Components>
<Databases>
<Database Name="Main" Connection="Data Source=.\SQL2008" />
<Database Name="Staging" Connection="Data Source=Internal" />
</Databases>
</Profile>
</Profiles>
So a collection of Profile with each profile having a collection of sub elements (Components is a collection, then Component is an element)
However I currently have all that EXCEPT for the multiple profiles. I kind of do see the problem but not sure how to "fix" it.
Code:
public class Profile : ConfigurationSection
{
[ConfigurationProperty("Name", IsRequired=true)]
public string Name
{
get
{
return base["Name"] as string;
}
set
{
base["Name"] = value;
}
}
[ConfigurationProperty("Components")]
public ComponentCollection Components
{
get { return ((ComponentCollection)(base["Components"])); }
}
[ConfigurationProperty("Databases")]
public DatabaseCollection Databases
{
get
{
return ((DatabaseCollection)(base["Databases"]));
}
}
}
[ConfigurationCollection(typeof(Component), AddItemName="Component")]
public class ComponentCollection : ConfigurationElementCollection
{
protected override ConfigurationElement CreateNewElement()
{
return new Component();
}
protected override object GetElementKey(ConfigurationElement element)
{
return ((Component)(element)).Name;
}
public Component this[int idx]
{
get
{
return base.BaseGet(idx) as Component;
}
set
{
if (base.BaseGet(idx) != null)
{
base.BaseRemoveAt(idx);
}
this.BaseAdd(idx, value);
}
}
public Component this[string key]
{
get
{
return base.BaseGet(key) as Component;
}
set
{
if (base.BaseGet(key) != null)
{
base.BaseRemove(key);
}
this.BaseAdd(this.Count, value);
}
}
}
public class Component : ConfigurationElement
{
[ConfigurationProperty("Type", IsRequired = true)]
public string Type
{
get
{
return this["Type"] as string;
}
set
{
this["Type"] = value;
}
}
[ConfigurationProperty("Name", IsRequired = true, IsKey = true)]
public string Name
{
get
{
return this["Name"] as string;
}
set
{
this["Name"] = value;
}
}
[ConfigurationProperty("ServiceName", IsRequired = false)]
public string ServiceName
{
get
{
return this["ServiceName"] as string;
}
set
{
this["ServiceName"] = value;
}
}
}
[ConfigurationCollection(typeof(Database), AddItemName = "Database")]
public class DatabaseCollection : ConfigurationElementCollection
{
protected override ConfigurationElement CreateNewElement()
{
return new Database();
}
protected override object GetElementKey(ConfigurationElement element)
{
return ((Database)(element)).Name;
}
public Database this[int idx]
{
get
{
return base.BaseGet(idx) as Database;
}
set
{
if (base.BaseGet(idx) != null)
{
base.BaseRemoveAt(idx);
}
this.BaseAdd(idx, value);
}
}
public Database this[string key]
{
get
{
return base.BaseGet(key) as Database;
}
set
{
if (base.BaseGet(key) != null)
{
base.BaseRemove(key);;
}
this.BaseAdd(this.Count, value);
}
}
}
public class Database : ConfigurationElement
{
[ConfigurationProperty("Name", IsKey = true, IsRequired = true)]
public string Name
{
get
{
return this["Name"] as string;
}
set
{
this["Name"] = value;
}
}
[ConfigurationProperty("Connection", IsKey = false, IsRequired = true)]
public string Connection
{
get
{
return this["Connection"] as string;
}
set
{
this["Connection"] = value;
}
}
}
You need to move your configuration section one level higher.
public class Profiles : ConfigurationSection
{
[ConfigurationProperty("Profile")]
public ProfileCollection Profile
{
get
{
return this["profile"] as ProfileCollection;
}
}
}
Here's a section that I created. You should be able to get yours working by following this:
public class ImportConfiguration : ConfigurationSection
{
[ConfigurationProperty("importMap")]
public ImportMapElementCollection ImportMap
{
get
{
return this["importMap"] as ImportMapElementCollection;
}
}
}
public class ImportColumnMapElement : ConfigurationElement
{
[ConfigurationProperty("localName", IsRequired = true, IsKey = true)]
public string LocalName
{
get
{
return this["localName"] as string;
}
set
{
this["localName"] = value;
}
}
[ConfigurationProperty("sourceName", IsRequired = true)]
public string SourceName
{
get
{
return this["sourceName"] as string;
}
set
{
this["sourceName"] = value;
}
}
}
public class ImportMapElementCollection : ConfigurationElementCollection
{
public ImportColumnMapElement this[object key]
{
get
{
return base.BaseGet(key) as ImportColumnMapElement;
}
}
public override ConfigurationElementCollectionType CollectionType
{
get
{
return ConfigurationElementCollectionType.BasicMap;
}
}
protected override string ElementName
{
get
{
return "columnMap";
}
}
protected override bool IsElementName(string elementName)
{
bool isName = false;
if (!String.IsNullOrEmpty(elementName))
isName = elementName.Equals("columnMap");
return isName;
}
protected override ConfigurationElement CreateNewElement()
{
return new ImportColumnMapElement();
}
protected override object GetElementKey(ConfigurationElement element)
{
return ((ImportColumnMapElement)element).LocalName;
}
}
这篇关于自定义配置的app.config - 部分藏品?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!