private Dictionary <string, PropertyInfoDto> GetPropertiesWithAction(Type type, Func <PropertyInfoDto, bool> whereAction)
    {
        var result = type.GetProperties()
                     .Select(p =>
        {
            var dto              = new PropertyInfoDto();
            dto.PropertyName     = p.Name;
            dto.DisplayAttribute = p.GetCustomAttributes(typeof(DisplayAttribute), false)?.FirstOrDefault() as DisplayAttribute;
            dto.ValidationRules  = GetValidationRules(p);
            dto.PropertyInfo     = p;
            dto.ElementType      = new[]
            {
                p.PropertyType.GetElementType(),
                p.PropertyType.GetGenericArguments().FirstOrDefault(gt => gt.IsValueType == false)
            }.FirstOrDefault(t => t != null);

            dto.IsClassObject = dto.IsCollection == false &&
                                dto.PropertyInfo.PropertyType.IsValueType == false &&
                                dto.PropertyInfo.PropertyType != typeof(string);

            if (dto.IsClassObject)
            {
                dto.ObjectType = dto.PropertyInfo.PropertyType;
            }

            return(dto);
        })
                     .Where(whereAction)
                     .ToDictionary(k => k.PropertyName,
                                   v => v);

        return(result);
    }
Ejemplo n.º 2
0
        private List <PropertyInfoDto> GetPropertyInfoFromEntityType(Type type)
        {
            var properties = type.GetProperties().Where(p => !p.IsDefined(typeof(NotMappedAttribute)));

            var list = new List <PropertyInfoDto>();

            foreach (var item in properties)
            {
                var displayName  = item.GetCustomAttribute <DisplayNameAttribute>()?.DisplayName;
                var propertyInfo = new PropertyInfoDto
                {
                    Name         = item.Name + (displayName.IsNullOrWhiteSpace()?"":$"({displayName})"),
                    EntityType   = type.AssemblyQualifiedName,
                    propertyName = item.Name,
                    FieldKind    = typeof(IEntityRoot).IsAssignableFrom(item.PropertyType)? FieldKind.Reference :typeof(string) != item.PropertyType && IsAssignableToGenericType(item.PropertyType, typeof(IEnumerable <>)) ? FieldKind.ICollection : FieldKind.ValueType,
                    PropertyType = item.PropertyType.AssemblyQualifiedName
                };
                if (propertyInfo.FieldKind != FieldKind.Reference)
                {
                    propertyInfo.Leaf = true;
                }
                if (propertyInfo.FieldKind == FieldKind.ICollection)
                {
                    propertyInfo.GenericType = item.PropertyType.GetGenericArguments()[0].AssemblyQualifiedName;
                }
                //以下如果循环引用导致overflow exception
                //if(propertyInfo.FieldKind==FieldKind.Reference)
                //{
                //    propertyInfo.Children = GetPropertyInfoFromEntityType(item.PropertyType);
                //}
                //if(propertyInfo.FieldKind==FieldKind.ICollection)
                //{
                //    var genericType = item.PropertyType.GetGenericArguments().First();
                //    propertyInfo.Children = GetPropertyInfoFromEntityType(genericType);
                //}
                list.Add(propertyInfo);
            }
            return(list);
        }