Как элегантно реализовать статические одноэлементные дженерики в С# ⇐ C#
-
Anonymous
Как элегантно реализовать статические одноэлементные дженерики в С#
I have some static classes bound to configuration files. Is there any better way so that I don’t have to write the GetValue method repeatedly every time?
public class ApiPath { private static readonly string Path = "Config/ApiPath.json"; #region Get Config Method public static IConfiguration? Configuration { get; private set; } public static ConfigValue GetValue() { if (Configuration == null) Configuration = new ConfigurationBuilder().AddJsonFile(Path, true, true).Build(); var model = new ConfigValue(); Configuration.Bind(model); return model; } #endregion public string GetCookieAPi { get; set; } public string CheckCookieAPi{ get; set; } } public class ConfigValue { private static readonly string Path = "Config/Config.json"; #region Get Config Method public static IConfiguration? Configuration { get; private set; } public static ConfigValue GetValue() { if (Configuration == null) Configuration = new ConfigurationBuilder().AddJsonFile(Path, true, true).Build(); var model = new ConfigValue(); Configuration.Bind(model); return model; } #endregion public string ServerIP { get; set; } public string MongoDbStr { get; set; } public int CookieIntervals { get; set; } } I use inheritance or generics, but I always encounter problems with static member ownership.
Источник: https://stackoverflow.com/questions/781 ... in-c-sharp
I have some static classes bound to configuration files. Is there any better way so that I don’t have to write the GetValue method repeatedly every time?
public class ApiPath { private static readonly string Path = "Config/ApiPath.json"; #region Get Config Method public static IConfiguration? Configuration { get; private set; } public static ConfigValue GetValue() { if (Configuration == null) Configuration = new ConfigurationBuilder().AddJsonFile(Path, true, true).Build(); var model = new ConfigValue(); Configuration.Bind(model); return model; } #endregion public string GetCookieAPi { get; set; } public string CheckCookieAPi{ get; set; } } public class ConfigValue { private static readonly string Path = "Config/Config.json"; #region Get Config Method public static IConfiguration? Configuration { get; private set; } public static ConfigValue GetValue() { if (Configuration == null) Configuration = new ConfigurationBuilder().AddJsonFile(Path, true, true).Build(); var model = new ConfigValue(); Configuration.Bind(model); return model; } #endregion public string ServerIP { get; set; } public string MongoDbStr { get; set; } public int CookieIntervals { get; set; } } I use inheritance or generics, but I always encounter problems with static member ownership.
Источник: https://stackoverflow.com/questions/781 ... in-c-sharp
Мобильная версия