AppSettingsHelper.cs 1.6 KB

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