我想分发一个带有 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/