Advertisement
Guest User

Untitled

a guest
May 26th, 2019
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.84 KB | None | 0 0
  1. public class PropertyDescriptionAttribute : Attribute {
  2. public string Description { get; set; }
  3. /* E.g
  4. public class MyObject {
  5. [PropertyDescriptionAttribute( Description = "my Id property description.")]
  6. public string Id { get; set; }
  7. }
  8. */
  9. }
  10.  
  11. public interface IObjectDescriptor : IDependency {
  12. dynamic Describe(Type type);
  13. }
  14.  
  15. public class ObjectDescriptor : IObjectDescriptor {
  16. Type[] ExludedClasses {
  17. get {
  18. return new[] {
  19. typeof(string),
  20. typeof(DateTime),
  21. typeof(int),
  22. typeof(decimal),
  23. typeof(double),
  24. typeof(char),
  25. typeof(bool)
  26. };
  27. }
  28. }
  29.  
  30. Type[] ExludedArrays {
  31. get {
  32. return new[] {
  33. typeof(string[]),
  34. typeof(DateTime[]),
  35. typeof(int[]),
  36. typeof(decimal[]),
  37. typeof(double[]),
  38. typeof(char[]),
  39. typeof(bool[])
  40. };
  41. }
  42. }
  43.  
  44. dynamic CurrentDescribingObject { get; set; }
  45.  
  46. public dynamic Describe(Type type) {
  47. if (type == null)
  48. return null;
  49.  
  50. CurrentDescribingObject = new ExpandoObject();
  51.  
  52. CurrentDescribingObject.Name = type.Name;
  53. CurrentDescribingObject.Properties = new List<dynamic>();
  54.  
  55. foreach (var property in type.GetProperties()) {
  56. CurrentDescribingObject.Properties.Add(DescribeProperty(property));
  57. }
  58.  
  59. return CurrentDescribingObject;
  60. }
  61.  
  62. dynamic DescribeProperty(PropertyInfo Property) {
  63. dynamic propertyDescriptor = new ExpandoObject();
  64. propertyDescriptor.Name = Property.Name;
  65. propertyDescriptor.Type = Property.PropertyType.Name;
  66. propertyDescriptor.Description = PropertyDescription(Property);
  67.  
  68. if (((List<dynamic>)CurrentDescribingObject.Properties).Count(r => r.Type == Property.PropertyType.Name && r.Name == Property.Name) > 0) {
  69. return ((List<dynamic>)CurrentDescribingObject.Properties).Where(r => r.Type == Property.PropertyType.Name && r.Name == Property.Name)
  70. .FirstOrDefault();
  71. }
  72.  
  73. if (Property.PropertyType.IsValueType || ExludedClasses.Contains(Property.PropertyType))
  74. return propertyDescriptor;
  75.  
  76. if (IsPropertyACollection(Property)) { /* array */
  77. if (!ExludedArrays.Contains(Property.PropertyType)) {
  78. var TypeArg = Property.PropertyType.GetGenericArguments().Count() > 0 ?
  79. Property.PropertyType.GetGenericArguments()[0] : Property.PropertyType.GetElementType();
  80.  
  81. propertyDescriptor.Name = $"array of {TypeArg.Name} (class)";
  82. propertyDescriptor.Type = $"{TypeArg.Name}[]";
  83. propertyDescriptor.Properties = new List<dynamic>();
  84.  
  85.  
  86. propertyDescriptor.Properties.Add(new {
  87. GenericTypeName = TypeArg.Name,
  88. Properties = ClassProperties(TypeArg)
  89. });
  90.  
  91. }
  92. } else if (IsClass(Property)) {
  93. propertyDescriptor.Name = $"{Property.PropertyType.Name} (class)";
  94. propertyDescriptor.TypeProperties = ClassProperties(Property.PropertyType);
  95. }
  96.  
  97. return propertyDescriptor;
  98. }
  99.  
  100.  
  101. IList<dynamic> ClassProperties(Type classType) {
  102. dynamic TypeProperties = new List<dynamic>();
  103. foreach (var property in classType.GetProperties()) {
  104. if (property.PropertyType.Name == classType.Name)
  105. continue;
  106.  
  107. TypeProperties.Add(DescribeProperty(property));
  108. }
  109. return TypeProperties;
  110. }
  111.  
  112. bool IsPropertyACollection(PropertyInfo property) {
  113. try {
  114. return property.PropertyType.GetInterface(typeof(IEnumerable<>).FullName) != null ||
  115. property.PropertyType.GetGenericTypeDefinition() == (typeof(IEnumerable<>));
  116. }
  117. catch {
  118. return false;
  119. }
  120. }
  121.  
  122. bool IsClass(PropertyInfo property) {
  123. return property.PropertyType.IsClass && !ExludedClasses.Contains(property.PropertyType);
  124. }
  125.  
  126. string PropertyDescription(PropertyInfo Property) {
  127. var attr = (PropertyDescriptionAttribute)Attribute.GetCustomAttribute(Property, typeof(PropertyDescriptionAttribute));
  128. if (attr == null)
  129. return "No description provided to this property :'(";
  130.  
  131. return attr.Description;
  132. }
  133. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement