c# - 可扩展的.NET配置

标签 c# .net oop design-patterns

我想分发一个带有 ConfigurationSection 的 DLL如下:

public class StandardConfiguration : ConfigurationSection
{
    public static StandardConfiguration GetInstance()
    {
        return (StandardConfiguration)ConfigurationManager.GetSection("customConfigSection");
    }

    [ConfigurationProperty("childConfig")]
    public StandardChildConfig ChildConfig
    {
        get { return (StandardChildConfig)this["childConfig"]; }
        set { this["childConfig"] = value; }
    }
}

public class StandardChildConfig : ConfigurationElement
{
    [ConfigurationProperty("p1")]
    public string P1
    {
        get { return (string)this["p1"]; }
        set { this["p1"] = value; }
    }
}

我想做ConfigurationSection及其子项 ConfigElement可继承的。这可以使用类型参数来完成,如下所示:

public class StandardConfiguration<TChildConfig> : ConfigurationSection
    where TChildConfig : StandardChildConfig
{
    [ConfigurationProperty("childConfig")]
    public TChildConfig ChildConfig
    {
        get { return (TChildConfig)this["childConfig"]; }
        set { this["childConfig"] = value; }
    }
}

public class StandardChildConfig : ConfigurationElement
{
    [ConfigurationProperty("p1")]
    public string P1
    {
        get { return (string)this["p1"]; }
        set { this["p1"] = value; }
    }
}

但是,我认为这会阻止我拥有静态 Instance从我的 DLL 中的其他类引用,因为我不知道子 ConfigurationElement 的最终类型.

欢迎就如何更干净地实现这一点提出任何想法或建议。

谢谢。

编辑

假设有一个<customConfigSection>在应用程序的配置中,我可以使用 StandardConfiguration.GetInstance().ChildConfig.P1访问P1第一种情况下的值。在第二种情况下我将如何访问相同的值?我将如何实现GetInstance()

编辑2

以下是“零编码”场景:

<?xml version="1.0"?>
<configuration>
    <configSections>
        <section
            name="customConfig"
            type="WebsiteTemplate.Config.StandardConfigruation, WebsiteTemplate"
        />
    </configSections>
    <customConfig baseProp1="a">
        <childConfig baseProp2="b" />
    </customConfig>
</configuration>

这是配置扩展的场景:

<?xml version="1.0"?>
<configuration>
    <configSections>
        <section
            name="customConfig"
            type="WebsiteTemplate.Extended.Config.ExtendedConfigruation, WebsiteTemplate.Extended"
        />
    </configSections>
    <customConfig baseProp1="a" extendedProp1="c">
        <childConfig baseProp2="b" extendedProp2="d" />
    </customConfig>
</configuration>

最佳答案

在第二个例子中StandardConfiguration.GetInstance()没有任何意义,因为 StandardConfiguraiton是通用的。你必须使用StandardConfiguration<MyChildConfig>.GetInstance().ChildConfig.P1

你也许可以做这样的事情:

public class StandardConfigurationBase : ConfigurationSection
{
    public static StandardConfigurationBase GetInstance()
    {
        return (StandardConfigurationBase) ConfigurationManager.GetSection("customConfigSection");
    }

    [ConfigurationProperty("childConfig")]
    public StandardChildConfig ChildConfig
    {
        get { return (StandardChildConfig) this["childConfig"]; }
        set { this["childConfig"] = value; }
    }
}

public class StandardConfiguration<TChildConfig> : StandardConfigurationBase
where TChildConfig : StandardChildConfig
{
    [ConfigurationProperty("childConfig")]
    public new TChildConfig ChildConfig
    {
        get { return (TChildConfig)this["childConfig"]; }
        set { this["childConfig"] = value; }
    }
}
public class StandardChildConfig : ConfigurationElement
{
    [ConfigurationProperty("p1")]
    public string P1
    {
        get { return (string)this["p1"]; }
        set { this["p1"] = value; }
    }
}

然后在未知其特定类型时访问子级:

    StandardConfigurationBase b = new StandardConfiguration<StandardChildConfig>();
    StandardChildConfig x = StandardConfigurationBase.GetInstance().ChildConfig;

但是,我不清楚这样做的真正值(value)。

关于c# - 可扩展的.NET配置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14573564/

相关文章:

c# - 编译器是否优化了字符串文字?

c# - 如何动态更改 WPF 中 GridView 列的顺序?

Java 实例化实现接口(interface)的泛型类型

entity-framework - 什么时候应该在 EF4 中使用 POCO?

oop - Dart 中的抽象基类

c# - 在 MVVM 中定义从模型到 View 的属性更改的理想方法是什么?

c# - 使用 ContinueOnError 批量插入

c# - 如何更改 ClickOnce 中先决条件的下载和安装顺序

.Net 停止监听套接字

c# - "Non-nullable event must contain a non-null value when exiting constructor"