Func.cs 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. using bitforum.Models;
  2. using Microsoft.AspNetCore.Mvc;
  3. using System.ComponentModel.DataAnnotations;
  4. using System.Reflection;
  5. namespace bitforum.Helpers
  6. {
  7. public static class Func
  8. {
  9. // SaveConfig 함수는 Config 모델에 대한 정보를 저장하는 함수
  10. public static void SaveConfig<T>(T request, Action<Config> saveAction)
  11. {
  12. var properties = typeof(T).GetProperties();
  13. foreach (var property in properties)
  14. {
  15. // 속성 Key 이름
  16. var key = property.GetCustomAttribute<BindPropertyAttribute>()?.Name ?? property.Name;
  17. // 속성 값
  18. var value = property.GetValue(request)?.ToString();
  19. // 속성 표시 이름
  20. var displayName = property.GetCustomAttribute<DisplayAttribute>()?.Name ?? key;
  21. saveAction(new Config
  22. {
  23. Key = key,
  24. Value = value?.ToString(),
  25. Description = displayName
  26. });
  27. }
  28. }
  29. // Config 값 조회
  30. public static string GetConfig(this Dictionary<string, string> dictionary, string key)
  31. {
  32. if (dictionary != null && dictionary.ContainsKey(key))
  33. {
  34. return dictionary[key];
  35. }
  36. return string.Empty;
  37. }
  38. }
  39. }