AppSettingsHelper.cs 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. using Microsoft.Extensions.Configuration;
  2. using Microsoft.Extensions.Configuration.Memory;
  3. using MySqlX.XDevAPI;
  4. namespace OASystem.Infrastructure.Tools;
  5. /// <summary>
  6. /// 配置帮助类
  7. /// </summary>
  8. public class AppSettingsHelper
  9. {
  10. static IConfiguration _config;
  11. public AppSettingsHelper(IConfiguration configuration)
  12. {
  13. _config = configuration;
  14. }
  15. public static string Get(string key, bool IsConn = false)
  16. {
  17. string value;
  18. try
  19. {
  20. if (key.IsNull()) return null;
  21. if (IsConn)
  22. {
  23. value = _config.GetConnectionString(key);
  24. }
  25. else
  26. {
  27. value = _config[key];
  28. }
  29. }
  30. catch (Exception)
  31. {
  32. value = null;
  33. }
  34. return value;
  35. }
  36. public static string Get(params string[] sessions)
  37. {
  38. try
  39. {
  40. if (sessions.Any())
  41. {
  42. return _config[string.Join(":", sessions)];
  43. }
  44. }
  45. catch
  46. {
  47. return null;
  48. }
  49. return null;
  50. }
  51. public static string Get(string key)
  52. {
  53. try
  54. {
  55. if (key.IsNull()) return null;
  56. return _config[key];
  57. }
  58. catch
  59. {
  60. return null;
  61. }
  62. }
  63. public static List<T> Get<T>(params string[] session)
  64. {
  65. var list = new List<T>();
  66. _config.Bind(string.Join(":", session), list);
  67. return list;
  68. }
  69. public static IConfigurationSection GetSection(string key)
  70. {
  71. try
  72. {
  73. return _config.GetSection(key);
  74. }
  75. catch
  76. {
  77. return null;
  78. }
  79. }
  80. }