Advertisement
Guest User

Untitled

a guest
Aug 30th, 2016
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.58 KB | None | 0 0
  1. public class DisplayData
  2. {
  3. private static DisplayData instance = null;
  4.  
  5. private DisplayData() { }
  6.  
  7. public static DisplayData Instance
  8. {
  9. get
  10. {
  11. if (instance == null)
  12. instance = new DisplayData();
  13.  
  14. return instance;
  15. }
  16.  
  17. }
  18. /// <summary>
  19. /// Get Attibute alias from Data annotation. If no exist Display(Name=) into attribute,
  20. /// this method return name of attributes
  21. /// </summary>
  22. /// <param name="objClass">class parent</param>
  23. /// <param name="propertyName"> attribute name</param>
  24. /// <returns>Alias name</returns>
  25. public string GetAttributeAlias(object objClass, string propertyName)
  26. {
  27.  
  28. var attrType = typeof(DisplayAttribute);
  29. var property = objClass.GetType().GetProperty(propertyName);
  30.  
  31. if (property.CustomAttributes.Count() > 0)
  32. return ((DisplayAttribute)property.GetCustomAttributes(attrType, false).First()).Name;
  33. else
  34. return property.Name;
  35. }
  36. /// <summary>
  37. /// Get Attibute value.
  38. /// </summary>
  39. /// <param name="objClass">class parent</param>
  40. /// <param name="propertyName"> attribute name</param>
  41. /// <returns>attribute value</returns>
  42. public object GetAttributeValue(object objClass, string propertyName)
  43. {
  44. var property = objClass.GetType().GetProperty(propertyName);
  45. return property.GetValue(objClass, null);
  46. }
  47. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement