ObjectExtension.cs 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Collections;
  6. using System.Collections.Specialized;
  7. using System.Reflection;
  8. using Newtonsoft.Json;
  9. namespace FMSAdmin.Helpers {
  10. public static class ObjectExtension {
  11. /*
  12. * Property 값을 가져옴
  13. * @param obj 객체
  14. * @param propName Property명
  15. **/
  16. public static object GetPropValue(this object obj, string propName) {
  17. Type type = obj.GetType();
  18. var prop = type.GetProperty(propName);
  19. if (prop == null) return null;
  20. return prop.GetValue(obj, null);
  21. }
  22. /*
  23. * Property 값을 설정
  24. * @param obj 객체
  25. * @param propName Property명
  26. * @param value 값
  27. **/
  28. public static void SetPropValue(this object obj, string propName, object value) {
  29. Type type = obj.GetType();
  30. var prop = type.GetProperty(propName);
  31. if (prop != null) prop.SetValue(obj, value, null);
  32. }
  33. public static bool IsNullProp(this PropertyInfo obj, object value) {
  34. bool isnull = false;
  35. try {
  36. object o = obj.GetValue(value, null);
  37. if (o == null) {
  38. isnull = true;
  39. }
  40. } catch (Exception) {
  41. isnull = true;
  42. }
  43. return isnull;
  44. }
  45. /*
  46. * 객체내부를 덤프
  47. * @param obj 객체
  48. **/
  49. public static string Dump(this object obj) {
  50. if (obj == null) return "null";
  51. return System.Text.Json.JsonSerializer.Serialize(obj);
  52. }
  53. }
  54. }