我如何使用的后代元素.NET自定义的ConfigurationElement属性? [英] How do I use .NET custom ConfigurationElement properties on descendent elements?
问题描述
我如何获得并使用在后代CustomSetting元素的父配置节设置属性?我需要的时候CustomSetting元素返回Value属性这个属性。
我想格式化App.config中是这样的:
< CustomSettings someProperty =富>
< CustomSetting键=栏值=fermeneba/>
< CustomSetting键=左心耳值=jubaduba/>
< / CustomSettings>
我有code的工作,但我不能找到一种方法,从CustomSetting类访问someProperty属性。我发现的唯一方法,到目前为止,是格式化这样的结构,这是混乱的:
< CustomSettings>
< CustomSetting someProperty =富键=栏值=fermeneba/>
< CustomSetting someProperty =富键=左心耳值=jubaduba/>
< / CustomSettings>
实现,这是更困难比它应该是因为System.Configuration API不允许您从导航的ConfigurationElement
其父。因此,如果您要访问的一些信息,一个父元素,你需要手动创建关系。我已经把一个样本实现,它对于你的问题的配置片断:
公共类CustomSettingsSection:配置节
{
[的ConfigurationProperty(someProperty,默认值=)]
公共字符串SomeProperty
{
{返回(串)基地[someProperty]; }
集合{基地[someProperty] =值; }
}
[的ConfigurationProperty(,IsDefaultCollection =真)
公共CustomSettingElementCollection元素
{
得到
{
VAR元素=基地()作为CustomSettingElementCollection;
如果(元素= NULL和放大器;!&安培; elements.Section == NULL)
elements.Section =这一点;
返回元素;
}
}
}
公共类CustomSettingElementCollection:ConfigurationElementCollection
{
内部CustomSettingsSection科{获得;组; }
公众覆盖ConfigurationElementCollectionType CollectionType
{
{返回ConfigurationElementCollectionType.BasicMap; }
}
公共CustomSettingElement此[字符串键]
{
{返回BaseGet(键)为CustomSettingElement; }
}
保护覆盖的ConfigurationElement CreateNewElement()
{
返回新CustomSettingElement {父=这};
}
保护覆盖对象GetElementKey(的ConfigurationElement元)
{
返回(元素CustomSettingElement)。重点;
}
保护覆盖字符串的ElementName
{
{返回customSetting; }
}
}
公共类CustomSettingElement:的ConfigurationElement
{
内部CustomSettingElementCollection家长{获得;组; }
公共字符串SomeProperty
{
得到
{
如果(家长= NULL和放大器;!&安培;!Parent.Section = NULL)
返回Parent.Section.SomeProperty;
返回默认值(字符串);
}
}
[的ConfigurationProperty(钥匙,IsKey = TRUE,IsRequired =真)
公共字符串键
{
{返回(串)基地[钥匙]。 }
集合{基地[钥匙] =价值; }
}
[的ConfigurationProperty(价值,默认值=)]
公共字符串值
{
{返回(串)基地[值]; }
集合{基地[值] =值; }
}
}
您可以看到 CustomSettingElementCollection
有一个科
属性,它得到了部分的元素
吸气。该 CustomSettingElement
,反过来,有一个父
属性,该属性获取集合的 CreateNewElement设置( )
方法。
这则使得可以走了关系树到 SomeProperty
属性添加到元素,即使这一个不符合实际的ConfigurationProperty上元素。
希望给你一个想法如何解决您的问题!
How can I get and use an attribute set in the parent ConfigurationSection in the descendent CustomSetting element? I need this attribute when the CustomSetting element is returning the Value property.
I want to format the App.config like this:
<CustomSettings someProperty="foo">
<CustomSetting key="bar" value="fermeneba" />
<CustomSetting key="laa" value="jubaduba" />
</CustomSettings>
I have the code working, except that I cannot find a way to access the someProperty attribute from the CustomSetting class. The only way that I've found, so far, is to format the configuration like this, which is messy:
<CustomSettings>
<CustomSetting someProperty="foo" key="bar" value="fermeneba" />
<CustomSetting someProperty="foo" key="laa" value="jubaduba" />
</CustomSettings>
Achieving this is more difficult than it should be since the System.Configuration API doesn't allow you to navigate from a ConfigurationElement
to its parent. Hence, if you want to access some information that on a parent element you need to create that relationship manually. I've put together a sample implementation that does that for the config snippet in your question:
public class CustomSettingsSection : ConfigurationSection
{
[ConfigurationProperty("someProperty", DefaultValue="")]
public string SomeProperty
{
get { return (string)base["someProperty"]; }
set { base["someProperty"] = value; }
}
[ConfigurationProperty("", IsDefaultCollection = true)]
public CustomSettingElementCollection Elements
{
get
{
var elements = base[""] as CustomSettingElementCollection;
if (elements != null && elements.Section == null)
elements.Section = this;
return elements;
}
}
}
public class CustomSettingElementCollection : ConfigurationElementCollection
{
internal CustomSettingsSection Section { get; set; }
public override ConfigurationElementCollectionType CollectionType
{
get { return ConfigurationElementCollectionType.BasicMap; }
}
public CustomSettingElement this[string key]
{
get { return BaseGet(key) as CustomSettingElement; }
}
protected override ConfigurationElement CreateNewElement()
{
return new CustomSettingElement { Parent = this };
}
protected override object GetElementKey(ConfigurationElement element)
{
return (element as CustomSettingElement).Key;
}
protected override string ElementName
{
get { return "customSetting"; }
}
}
public class CustomSettingElement : ConfigurationElement
{
internal CustomSettingElementCollection Parent { get; set; }
public string SomeProperty
{
get
{
if (Parent != null && Parent.Section != null)
return Parent.Section.SomeProperty;
return default(string);
}
}
[ConfigurationProperty("key", IsKey = true, IsRequired = true)]
public string Key
{
get { return (string)base["key"]; }
set { base["key"] = value; }
}
[ConfigurationProperty("value", DefaultValue = "")]
public string Value
{
get { return (string)base["value"]; }
set { base["value"] = value; }
}
}
You can see that the CustomSettingElementCollection
has a Section
property which gets set in the section's Elements
getter. The CustomSettingElement
, in turn, has a Parent
property which gets set in the collection's CreateNewElement()
method.
That then makes it possible to walk up the relationship tree and to add a SomeProperty
property to the element even though this one doesn't correspond to an actual ConfigurationProperty on that element.
Hope that gives you an idea how to solve your problem!
这篇关于我如何使用的后代元素.NET自定义的ConfigurationElement属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!