修改自定义配置部分子元素值

时间:2013-06-17 15:45:12

标签: c# configuration custom-configuration

我正在尝试删除一些旧的遗留引用,现在我正在处理以前从未尝试过的事情。假设我有一个这样的配置文件部分:

<customSection>
    <customValues>
      <custom key="foo" invert="True">
         <value>100</value>
      </custom>
      <custom key="bar" invert="False">
         <value>200</value>
      </custom>
    </customValues>
</customSection>

我现在已经创建了ConfigurationSection,ConfigurationElement和ConfigurationElementCollection类来正确读取所有这些内容。这里它们仅供参考(它基本上都是所有样板,除了ValueElement类,它覆盖了Deserialize方法以获得元素的值):

public class CustomSection : ConfigurationSection
{
    [ConfigurationProperty("customValues")]
    [ConfigurationCollection(typeof(CustomValueCollection), AddItemName = "custom")]
    public CustomValueCollection CustomValues
    {
        get { return (CustomValueCollection)this["customValues"]; }
    }
}

public class CustomValueCollection : ConfigurationElementCollection
{
    protected override ConfigurationElement CreateNewElement()
    {
        return new CustomElement();
    }

    protected override object GetElementKey(ConfigurationElement element)
    {
        return ((CustomElement) element).Key;
    }

    public CustomElement this[int index]
    {
        get { return (CustomElement) BaseGet(index); }
    }

    new public CustomElement this[string key]
    {
        get { return (CustomElement) BaseGet(key); }
    }

    public bool ContainsKey(string key)
    {
        var keys = new List<object>(BaseGetAllKeys());
        return keys.Contains(key);
    }
}

public class CustomElement : ConfigurationElement
{
    [ConfigurationProperty("key", IsRequired = true)]
    public string Key
    {
        get { return (string)this["key"]; }
    }

    [ConfigurationProperty("invert", IsRequired = true)]
    public bool Invert
    {
        get { return (bool)this["invert"]; }
    }

    [ConfigurationProperty("value", IsRequired = true)]
    public ValueElement Value
    {
        get { return (ValueElement)this["value"]; }
    }
}

public class ValueElement : ConfigurationElement
{
    private int value;

    //used to get value of element, not of an attribute
    protected override void DeserializeElement(System.Xml.XmlReader reader, bool serializeCollectionKey)
    {
        value = (int)reader.ReadElementContentAs(typeof(int), null);
    }

    public int Value
    {
        get { return value; }
    }
}

我现在坚持的是这个业务要求:如果CustomElement的反转值为true,则反转相关ValueElement中Value属性的值。所以,如果我在“foo”下访问“value”的值,我会得到-100。

有没有人知道如何将类似的东西传递给ValueElement对象,或者让ValueElement知道它的父CustomElement能够获得该Invert属性?我最初的想法是检查CustomElement类的Value属性getter,如果Invert为true,那么在那里修改ValueElement对象,但我对其他想法持开放态度。

这里的目标是在不触及配置文件的情况下删除遗留代码,否则我会将“value”子元素作为属性推送到父元素中。

由于

1 个答案:

答案 0 :(得分:1)

通过它看,你只需要修改Value属性getter来包含你的反转逻辑。我没有理由认为这不起作用。

您可以添加另一个获取原始值的属性。

[ConfigurationProperty("value", IsRequired = true)]
public int Value
{
    get 
    {
        var result = (ValueElement)this["value"]; 
        return Invert ? result.Value * -1 : result.Value;
    }
}
相关问题