public RelatedDtoProperty(RelatedDtoAttribute attribute, RelatedValueType dtoType, RelatedValueType idType) { Attribute = attribute; DtoType = dtoType; IdType = idType; BuildDtoListType(); }
private Dictionary <string, RelatedDtoProperty> BuildRules(Type targetDtoType) { var rules = new Dictionary <string, RelatedDtoProperty>(); var propsForRelatedDto = targetDtoType.GetProperties() .Select(x => new { Property = x, RelatedDtoAttribute = (RelatedDtoAttribute)x.GetCustomAttribute(typeof(RelatedDtoAttribute), true) }) .Where(x => x.RelatedDtoAttribute != null) .ToArray(); foreach (var propForRelatedDto in propsForRelatedDto) { var attribute = propForRelatedDto.RelatedDtoAttribute; var dtoProperty = propForRelatedDto.Property; var dtoPropertyType = dtoProperty.PropertyType; var dtoType = new RelatedValueType(dtoProperty); var idPropertyName = attribute.IdPropertyName; var isList = dtoType.GenericType != null || dtoType.IsArray; if (idPropertyName == null) { idPropertyName = isList ? $"{dtoPropertyType.Name}Ids" : $"{dtoProperty.Name}Id"; } PropertyInfo idProperty = null; if (!string.IsNullOrEmpty(idPropertyName)) { idProperty = _targetType.GetProperty(idPropertyName, BindingFlags.Public | BindingFlags.Instance); } if (idProperty == null && isList) { throw new MissingIdPropertyNameException(targetDtoType.Name, dtoProperty.Name); } var idType = idProperty == null ? null : new RelatedValueType(idProperty); var rule = new RelatedDtoProperty(attribute, dtoType, idType); rules.Add(dtoProperty.Name, rule); } return(rules); }