基于 app.config 值的不同 app.config appSettings
Different app.config appSettings based on app.config value
我有app.config
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<appSettings>
<!--One set of properties-->
<add key="Condition" value="A"/>
<add key="DependingPropertyA" value="B"/>
<add key="DependingPropertyB" value="C"/>
<!--Another set of properties-->
<add key="Condition" value="B"/>
<add key="DependingPropertyA" value="D"/>
<add key="DependingPropertyB" value="E"/>
</appSettings>
</configuration>
所以我想要 if Condition == A 定义一组属性,if Condition == B - 不同的属性集,这样当我在 A 和 B 之间切换时,我不需要更改所有其他属性。
可能吗?
如果每个 "Condition" 的 属性 名称都相同(也许是环境?),那么您可以通过一些编码技巧来做到这一点:
(App.Config:)
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<appSettings>
<!--Condition 'selector', change value to 'CondB' when you want to switch-->
<add key="Condition" value="CondA"/>
<add key="CondA.DependingPropertyA" value="B"/>
<add key="CondA.DependingPropertyB" value="C"/>
<add key="CondB.DependingPropertyA" value="D"/>
<add key="CondB.DependingPropertyB" value="E"/>
</appSettings>
</configuration>
然后,假设在您的 C# .NET 代码中:
string keyPrepend = System.Configuration.ConfigurationManager.AppSettings["Condition"];
string propAValue = System.Configuration.ConfigurationManager.AppSettings[String.Format("{0}.{1}", keyPrepend, "DependingPropertyA")];
string propBValue = System.Configuration.ConfigurationManager.AppSettings[String.Format("{0}.{1}", keyPrepend, "DependingPropertyB")];
这让您可以根据单个条件键的值切换要使用的 key/values 组。
我决定走这条路。
自定义部分:
using System.Configuration;
namespace ConditionalConfig
{
public class ConditionalConfigurationSection : ConfigurationSection
{
[ConfigurationProperty("selector", IsRequired = true, IsDefaultCollection = true)]
public string Selector
{
get { return (string) base["selector"]; }
set { base["selector"] = value; }
}
public new string this[string key]
{
get
{
var result = Shared[key] ?? ((KeyValueConfigurationCollection) base[Selector])[key];
return result != null ? result.Value : null;
}
}
[ConfigurationProperty("", IsRequired = true, IsDefaultCollection = true)]
public KeyValueConfigurationCollection Shared
{
get { return (KeyValueConfigurationCollection) base[""]; }
}
[ConfigurationProperty("opt1", IsRequired = true)]
public KeyValueConfigurationCollection Opt1
{
get { return (KeyValueConfigurationCollection) base["opt1"]; }
}
[ConfigurationProperty("opt2", IsRequired = true)]
public KeyValueConfigurationCollection Opt2
{
get { return (KeyValueConfigurationCollection) base["opt2"]; }
}
}
}
app.config:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="conditional" type="ConditionalConfig.ConditionalConfigurationSection, ConditionalConfig" />
</configSections>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
</startup>
<conditional selector="opt2">
<add key="test" value="value"/>
<opt1>
<add key="test1" value="value1"/>
</opt1>
<opt2>
<add key="test1" value="value2"/>
</opt2>
</conditional>
</configuration>
要是我能删除 "public KeyValueConfigurationCollection Opt1" 属性就好了 - 这对我来说太完美了。
我有app.config
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<appSettings>
<!--One set of properties-->
<add key="Condition" value="A"/>
<add key="DependingPropertyA" value="B"/>
<add key="DependingPropertyB" value="C"/>
<!--Another set of properties-->
<add key="Condition" value="B"/>
<add key="DependingPropertyA" value="D"/>
<add key="DependingPropertyB" value="E"/>
</appSettings>
</configuration>
所以我想要 if Condition == A 定义一组属性,if Condition == B - 不同的属性集,这样当我在 A 和 B 之间切换时,我不需要更改所有其他属性。 可能吗?
如果每个 "Condition" 的 属性 名称都相同(也许是环境?),那么您可以通过一些编码技巧来做到这一点:
(App.Config:)
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<appSettings>
<!--Condition 'selector', change value to 'CondB' when you want to switch-->
<add key="Condition" value="CondA"/>
<add key="CondA.DependingPropertyA" value="B"/>
<add key="CondA.DependingPropertyB" value="C"/>
<add key="CondB.DependingPropertyA" value="D"/>
<add key="CondB.DependingPropertyB" value="E"/>
</appSettings>
</configuration>
然后,假设在您的 C# .NET 代码中:
string keyPrepend = System.Configuration.ConfigurationManager.AppSettings["Condition"];
string propAValue = System.Configuration.ConfigurationManager.AppSettings[String.Format("{0}.{1}", keyPrepend, "DependingPropertyA")];
string propBValue = System.Configuration.ConfigurationManager.AppSettings[String.Format("{0}.{1}", keyPrepend, "DependingPropertyB")];
这让您可以根据单个条件键的值切换要使用的 key/values 组。
我决定走这条路。 自定义部分:
using System.Configuration;
namespace ConditionalConfig
{
public class ConditionalConfigurationSection : ConfigurationSection
{
[ConfigurationProperty("selector", IsRequired = true, IsDefaultCollection = true)]
public string Selector
{
get { return (string) base["selector"]; }
set { base["selector"] = value; }
}
public new string this[string key]
{
get
{
var result = Shared[key] ?? ((KeyValueConfigurationCollection) base[Selector])[key];
return result != null ? result.Value : null;
}
}
[ConfigurationProperty("", IsRequired = true, IsDefaultCollection = true)]
public KeyValueConfigurationCollection Shared
{
get { return (KeyValueConfigurationCollection) base[""]; }
}
[ConfigurationProperty("opt1", IsRequired = true)]
public KeyValueConfigurationCollection Opt1
{
get { return (KeyValueConfigurationCollection) base["opt1"]; }
}
[ConfigurationProperty("opt2", IsRequired = true)]
public KeyValueConfigurationCollection Opt2
{
get { return (KeyValueConfigurationCollection) base["opt2"]; }
}
}
}
app.config:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="conditional" type="ConditionalConfig.ConditionalConfigurationSection, ConditionalConfig" />
</configSections>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
</startup>
<conditional selector="opt2">
<add key="test" value="value"/>
<opt1>
<add key="test1" value="value1"/>
</opt1>
<opt2>
<add key="test1" value="value2"/>
</opt2>
</conditional>
</configuration>
要是我能删除 "public KeyValueConfigurationCollection Opt1" 属性就好了 - 这对我来说太完美了。