ConfigAdapter.cs 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Configuration;
  4. using System.Runtime.CompilerServices;
  5. namespace D3NsCore.Tools
  6. {
  7. internal class ConfigAdapter
  8. {
  9. private readonly Dictionary<string, string> _dicConf;
  10. public ConfigAdapter(Dictionary<string, string> dicConf)
  11. {
  12. _dicConf = dicConf;
  13. }
  14. private string GetValue([CallerMemberName] string confKey = "")
  15. {
  16. return _dicConf.ContainsKey(confKey)
  17. ? _dicConf[confKey]
  18. : null;
  19. }
  20. private string[] GetValues([CallerMemberName] string confKey = "")
  21. {
  22. // ReSharper disable once ExplicitCallerInfoArgument
  23. return (GetValue(confKey) ?? "").Split(new[] { "\r\n", "\r", "\n" }, StringSplitOptions.RemoveEmptyEntries);
  24. }
  25. private int GetInt32Value([CallerMemberName] string confKey = "")
  26. {
  27. // ReSharper disable once ExplicitCallerInfoArgument
  28. var value = GetValue(confKey);
  29. if (int.TryParse(value, out var int32Value))
  30. {
  31. return int32Value;
  32. }
  33. throw new ConfigurationErrorsException($"Bad config {confKey}");
  34. }
  35. public string Key => GetValue();
  36. public string Secret => GetValue();
  37. public string GetMyIp => GetValue();
  38. public string Domain => GetValue();
  39. public string DnsRecordName => GetValue();
  40. }
  41. }