Как элегантно реализовать статические одноэлементные дженерики в С# ⇐ C#
Как элегантно реализовать статические одноэлементные дженерики в С#
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
-
- Похожие темы
- Ответы
- Просмотры
- Последнее сообщение
-
-
Как правильно и элегантно реализовать функцию apply для структурированной привязки?
Anonymous » » в форуме C++ - 0 Ответы
- 48 Просмотры
-
Последнее сообщение Anonymous
-
-
-
Как правильно и элегантно реализовать функцию apply для структурированной привязки?
Anonymous » » в форуме C++ - 0 Ответы
- 63 Просмотры
-
Последнее сообщение Anonymous
-