CoreConfig.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. #if NETFRAMEWORK
  2. using System;
  3. using System.Configuration;
  4. using Newtonsoft.Json;
  5. namespace Masuit.Tools.Config
  6. {
  7. public static class ConfigHelper
  8. {
  9. public static string GetConfigOrDefault(string key, string defaultValue = "")
  10. {
  11. return ConfigurationManager.AppSettings.Get(key) ?? defaultValue;
  12. }
  13. public static T Get<T>(string key)
  14. {
  15. var value = ConfigurationManager.AppSettings.Get(key);
  16. if (string.IsNullOrWhiteSpace(value))
  17. {
  18. return default;
  19. }
  20. try
  21. {
  22. return JsonConvert.DeserializeObject<T>(value);
  23. }
  24. catch
  25. {
  26. return default;
  27. }
  28. }
  29. }
  30. }
  31. #else
  32. using Microsoft.Extensions.Configuration;
  33. using Newtonsoft.Json;
  34. using Newtonsoft.Json.Linq;
  35. using System;
  36. using System.Linq;
  37. namespace Masuit.Tools.Config
  38. {
  39. /// <summary>
  40. /// .net core的配置导入
  41. /// </summary>
  42. public static class ConfigHelper
  43. {
  44. /// <summary>
  45. /// 配置对象
  46. /// </summary>
  47. private static IConfiguration Configuration { get; set; } = new ConfigurationBuilder().SetBasePath(AppContext.BaseDirectory).AddJsonFile("appsettings.json", true, true).Build();
  48. public static string GetConfigOrDefault(string key, string defaultValue = "")
  49. {
  50. string config = Configuration[key];
  51. return config.IsNullOrEmpty() ? defaultValue : config;
  52. }
  53. public static T Get<T>(string key)
  54. {
  55. var section = Configuration?.GetSection(key);
  56. if (section == null)
  57. {
  58. return default;
  59. }
  60. if (!section.GetChildren().Any())
  61. {
  62. var value = section.Value ?? Configuration[key];
  63. if (string.IsNullOrEmpty(value))
  64. {
  65. return default;
  66. }
  67. return ConvertTo<T>(value);
  68. }
  69. var token = BuildToken(section);
  70. return token is null ? default : token.ToObject<T>(JsonSerializer.CreateDefault());
  71. }
  72. private static T ConvertTo<T>(string value)
  73. {
  74. var targetType = Nullable.GetUnderlyingType(typeof(T)) ?? typeof(T);
  75. try
  76. {
  77. if (targetType == typeof(string))
  78. {
  79. return (T)(object)value;
  80. }
  81. if (targetType.IsEnum)
  82. {
  83. return (T)Enum.Parse(targetType, value, true);
  84. }
  85. if (targetType == typeof(Guid))
  86. {
  87. return (T)(object)Guid.Parse(value);
  88. }
  89. if (targetType == typeof(bool))
  90. {
  91. if (bool.TryParse(value, out var b)) return (T)(object)b;
  92. if (int.TryParse(value, out var bi)) return (T)(object)(bi != 0);
  93. }
  94. if (targetType.IsPrimitive || targetType == typeof(decimal))
  95. {
  96. return (T)Convert.ChangeType(value, targetType);
  97. }
  98. }
  99. catch
  100. {
  101. }
  102. try
  103. {
  104. return JsonConvert.DeserializeObject<T>(value);
  105. }
  106. catch
  107. {
  108. return default;
  109. }
  110. }
  111. private static JToken BuildToken(IConfigurationSection section)
  112. {
  113. var children = section.GetChildren().ToList();
  114. if (children.Count == 0)
  115. {
  116. return section.Value != null ? JToken.FromObject(section.Value) : null;
  117. }
  118. if (children.All(c => int.TryParse(c.Key, out _)))
  119. {
  120. var array = new JArray();
  121. foreach (var child in children.OrderBy(c => int.Parse(c.Key)))
  122. {
  123. array.Add(BuildToken(child));
  124. }
  125. return array;
  126. }
  127. var obj = new JObject();
  128. foreach (var child in children)
  129. {
  130. obj[child.Key] = BuildToken(child);
  131. }
  132. return obj;
  133. }
  134. /// <summary>
  135. /// 将配置添加到Masuit.Tools,若未调用,将自动加载默认的appsettings.json
  136. /// </summary>
  137. /// <param name="config"></param>
  138. public static void AddToMasuitTools(this IConfiguration config)
  139. {
  140. Configuration = config;
  141. }
  142. }
  143. }
  144. #endif