【发布时间】:2020-09-04 20:54:52
【问题描述】:
我想用 json 设置文件转换我的静态类的反序列化。
public class Welcome
{
public ConfigurationProvider ConfigurationProvider { get; set; }
}
[JsonObject(MemberSerialization.OptIn)]
public class ConfigurationProvider
{
[JsonProperty(DefaultValueHandling = DefaultValueHandling.Populate)]
public static AppConfig Rcom { get; }
[JsonProperty(DefaultValueHandling = DefaultValueHandling.Populate)]
public static AppConfig Ccst { get; }
static ConfigurationProvider()
{
Rcom=new AppConfig("RCOM");
Ccst=new AppConfig("CCST");
}
}
public class AppConfig
{
public GarConfiguration GarConfiguration { get; }
public VacationRentalRedisKeysConfiguration VacationRentalRedisKeysConfiguration { get; }
public AppConfig(string type)
{
if (type == "RCOM")
{
GarConfiguration = new GarConfiguration(type);
VacationRentalRedisKeysConfiguration = new VacationRentalRedisKeysConfiguration
{
DefaultVacationRentalThemeKey = "RcomKey",
CheckVacationRentalThemeKey=true
};
}
else
{
GarConfiguration = new GarConfiguration(type);
VacationRentalRedisKeysConfiguration = new VacationRentalRedisKeysConfiguration
{
DefaultVacationRentalThemeKey = "CCSTKey",
CheckVacationRentalThemeKey=false
};
}
}
}
public class GarConfiguration
{
public GarSearch GarSearch { get; }
public GarConfiguration(string type)
{
if (type == "RCOM")
{
GarSearch = new GarSearch
{
RequestSplitedMinCount = 40,
RequestSplitedCount = 5,
RequestSplitedMaxTimeOutInMilliSecond = 3000,
RequiredSplitedRequest = true,
RequiredSplitedRequestMaxTimeOut = true,
AppName = type
};
}
else
{
GarSearch = new GarSearch
{
RequestSplitedMinCount = 40,
RequestSplitedCount = 5,
RequestSplitedMaxTimeOutInMilliSecond = 5000,
RequiredSplitedRequest = true,
RequiredSplitedRequestMaxTimeOut = true,
AppName = type
};
}
}
}
public class GarSearch
{
public long RequestSplitedMinCount { get; set; }
public long RequestSplitedCount { get; set; }
public long RequestSplitedMaxTimeOutInMilliSecond { get; set; }
public bool RequiredSplitedRequest { get; set; }
public bool RequiredSplitedRequestMaxTimeOut { get; set; }
public string AppName { get; set; }
}
public class VacationRentalRedisKeysConfiguration
{
public bool CheckVacationRentalThemeKey { get; set; }
public string DefaultVacationRentalThemeKey { get; set; }
}
上面是我的示例类。
我需要这个配置,因为如果我没有得到 JSON 设置文件,那么它将从这个默认设置运行,否则它将从那个 JSON 设置文件运行。
我需要这个静态的,因为不想为此创建多个实例。
当我反序列化我的课程时,我会得到类似的结果 class Deserialize proeprly but not able to access static member Not able to get static memeber values
【问题讨论】:
-
您应该将编程语言添加为标签
-
'我需要这个静态,因为不想为此创建多个实例':不合理。您必须在
static和可序列化之间进行选择。
标签: c# json serialization static-methods