C#プロファイルにカスタムsectionタグを追加してデータを取得
: section , :
1.タイプ「xxxx.xxxx.sectionName」は、「System.C onfiguration.IConfigurationSectionHandler」から継承されません.
この問題はよくある間違い問題と言えるはずだ.
解決策:
1.sectionラベルを追加します.nameはカスタムラベル名、typeは:タイプのネーミングスペース+タイプ、プログラムセット名
構成ノードの下に追加
<configSections>
<section name="RedisConfig" type="Redis.Study.Config.RedisConfigInfo,Redis.Study"/>
2.RedisConfigはカスタムラベルであり、彼のデータセットは(ConfigurationSection)である.
<RedisConfig WriteServerList="127.0.0.1:6379" ReadServerList="127.0.0.1:6379" MaxWritePoolSize="60"
MaxReadPoolSize="60" AutoStart="true" LocalCacheTime="180" RecordeLog="false"></RedisConfig>
</configSections>
3.カスタムラベルの取得クラスを作成します.ここではクラスの継承がデータセットコンフィギュレーション設定であることに注意してください.ここではクラスのプロパティとラベルのプロパティを使用する必要があります.コンフィギュレーションプロパティ(「ラベルプロパティ」)
public class RedisConfigInfo:ConfigurationSection
{
#region Property
[ConfigurationProperty("WriteServerList")]
public string WriteServerList {
get {
return (string)this["WriteServerList"]; }
set {
this["WriteServerList"] = value; }
}
[ConfigurationProperty("ReadServerList")]
public string ReadServerList
{
get {
return (string)this["ReadServerList"]; }
set {
this["ReadServerList"] = value; }
}
[ConfigurationProperty("MaxReadPoolSize")]
public int MaxReadPoolSize
{
get {
return (int)this["MaxReadPoolSize"]; }
set {
this["MaxReadPoolSize"] = value; }
}
[ConfigurationProperty("MaxWritePoolSize")]
public int MaxWritePoolSize
{
get {
return (int)this["MaxWritePoolSize"]; }
set {
this["MaxWritePoolSize"] = value; }
}
[ConfigurationProperty("AutoStart")]
public bool AutoStart
{
get {
return (bool)this["AutoStart"]; }
set {
this["AutoStart"] = value; }
}
[ConfigurationProperty("RecordeLog")]
public bool RecordeLog
{
get {
return (bool)this["RecordeLog"]; }
set {
this["RecordeLog"] = value; }
}
[ConfigurationProperty("LocalCacheTime")]
public int LocalCacheTime
{
get {
return (int)this["LocalCacheTime"]; }
set {
this["LocalCacheTime"] = value; }
}
#endregion
}
4.このような中に取得方法を追加してプロファイルSectionを取得して返されるsectionには、すべてのsection対応ラベル値が含まれている
#region
public static RedisConfigInfo GetConfig() {
RedisConfigInfo section = (RedisConfigInfo)ConfigurationManager.GetSection("RedisConfig");
return section;
}
public static RedisConfigInfo GetConfig(string sectionName) {
RedisConfigInfo section = (RedisConfigInfo)ConfigurationManager.GetSection(sectionName);
if (section == null) {
throw new ConfigurationErrorsException("Section " + sectionName + " is not found");
}
return section;
}
#endregion
5.プロセスで主に発生する問題は、継承問題とラベル属性の対応である.もちろんもっと細かい内容もたくさんあります.あとは一番補足しましょう