Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class PropertyDescriptionAttribute : Attribute {
- public string Description { get; set; }
- /* E.g
- public class MyObject {
- [PropertyDescriptionAttribute( Description = "my Id property description.")]
- public string Id { get; set; }
- }
- */
- }
- public interface IObjectDescriptor : IDependency {
- dynamic Describe(Type type);
- }
- public class ObjectDescriptor : IObjectDescriptor {
- Type[] ExludedClasses {
- get {
- return new[] {
- typeof(string),
- typeof(DateTime),
- typeof(int),
- typeof(decimal),
- typeof(double),
- typeof(char),
- typeof(bool)
- };
- }
- }
- Type[] ExludedArrays {
- get {
- return new[] {
- typeof(string[]),
- typeof(DateTime[]),
- typeof(int[]),
- typeof(decimal[]),
- typeof(double[]),
- typeof(char[]),
- typeof(bool[])
- };
- }
- }
- dynamic CurrentDescribingObject { get; set; }
- public dynamic Describe(Type type) {
- if (type == null)
- return null;
- CurrentDescribingObject = new ExpandoObject();
- CurrentDescribingObject.Name = type.Name;
- CurrentDescribingObject.Properties = new List<dynamic>();
- foreach (var property in type.GetProperties()) {
- CurrentDescribingObject.Properties.Add(DescribeProperty(property));
- }
- return CurrentDescribingObject;
- }
- dynamic DescribeProperty(PropertyInfo Property) {
- dynamic propertyDescriptor = new ExpandoObject();
- propertyDescriptor.Name = Property.Name;
- propertyDescriptor.Type = Property.PropertyType.Name;
- propertyDescriptor.Description = PropertyDescription(Property);
- if (((List<dynamic>)CurrentDescribingObject.Properties).Count(r => r.Type == Property.PropertyType.Name && r.Name == Property.Name) > 0) {
- return ((List<dynamic>)CurrentDescribingObject.Properties).Where(r => r.Type == Property.PropertyType.Name && r.Name == Property.Name)
- .FirstOrDefault();
- }
- if (Property.PropertyType.IsValueType || ExludedClasses.Contains(Property.PropertyType))
- return propertyDescriptor;
- if (IsPropertyACollection(Property)) { /* array */
- if (!ExludedArrays.Contains(Property.PropertyType)) {
- var TypeArg = Property.PropertyType.GetGenericArguments().Count() > 0 ?
- Property.PropertyType.GetGenericArguments()[0] : Property.PropertyType.GetElementType();
- propertyDescriptor.Name = $"array of {TypeArg.Name} (class)";
- propertyDescriptor.Type = $"{TypeArg.Name}[]";
- propertyDescriptor.Properties = new List<dynamic>();
- propertyDescriptor.Properties.Add(new {
- GenericTypeName = TypeArg.Name,
- Properties = ClassProperties(TypeArg)
- });
- }
- } else if (IsClass(Property)) {
- propertyDescriptor.Name = $"{Property.PropertyType.Name} (class)";
- propertyDescriptor.TypeProperties = ClassProperties(Property.PropertyType);
- }
- return propertyDescriptor;
- }
- IList<dynamic> ClassProperties(Type classType) {
- dynamic TypeProperties = new List<dynamic>();
- foreach (var property in classType.GetProperties()) {
- if (property.PropertyType.Name == classType.Name)
- continue;
- TypeProperties.Add(DescribeProperty(property));
- }
- return TypeProperties;
- }
- bool IsPropertyACollection(PropertyInfo property) {
- try {
- return property.PropertyType.GetInterface(typeof(IEnumerable<>).FullName) != null ||
- property.PropertyType.GetGenericTypeDefinition() == (typeof(IEnumerable<>));
- }
- catch {
- return false;
- }
- }
- bool IsClass(PropertyInfo property) {
- return property.PropertyType.IsClass && !ExludedClasses.Contains(property.PropertyType);
- }
- string PropertyDescription(PropertyInfo Property) {
- var attr = (PropertyDescriptionAttribute)Attribute.GetCustomAttribute(Property, typeof(PropertyDescriptionAttribute));
- if (attr == null)
- return "No description provided to this property :'(";
- return attr.Description;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement