Advertisement
retesere20

c# -- older-propertyget_

May 1st, 2019
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.77 KB | None | 0 0
  1.  
  2.  
  3. public dynamic propertyGet(object obj_, string propName, string type)
  4. {
  5. return propertyGet_static2(obj_, propName, type);
  6. }
  7.  
  8. public static dynamic propertyGet_static(object obj_, string propName)
  9. {
  10. return propertyGet_static2(obj_, propName, "");
  11. }
  12.  
  13. public static dynamic propertyGet_static2(object obj_, string propName, string type)
  14. {
  15. var propInfo = obj_.GetType().GetProperty(propName);
  16. if (propInfo != null)
  17. {
  18. if (type == "string")
  19. {
  20. return ((string)propInfo.GetValue(obj_, null));
  21. }
  22. else if (type == "bool")
  23. {
  24. return ((bool)propInfo.GetValue(obj_, null));
  25. }
  26. else if (type == "dictionary_string_string")
  27. {
  28. return ((Dictionary<string, string>)propInfo.GetValue(obj_, null));
  29. }
  30. return (dynamic)propInfo.GetValue(obj_, null);
  31. }
  32.  
  33. var fieldInfo = obj_.GetType().GetField(propName);
  34. if (fieldInfo != null)
  35. {
  36. if (type == "string")
  37. {
  38. return ((string)fieldInfo.GetValue(obj_));
  39. }
  40. else if (type == "bool")
  41. {
  42. return ((bool)fieldInfo.GetValue(obj_));
  43. }
  44. else if (type == "dictionary_string_string")
  45. {
  46. return ((Dictionary<string, string>)fieldInfo.GetValue(obj_));
  47. }
  48. return ((dynamic)propInfo.GetValue(obj_));
  49. }
  50.  
  51. return "";
  52. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement