private ReferenceTypeModel ConvertReferenceProperty(PropertyInfo property, IDictionary <string, List <Type> > overrides)
        {
            var referenceAttr = property.GetCustomAttribute <ResourceReferenceAttribute>();
            var displayName   = property.GetDisplayName();

            // Create reference model from property information and optional attribute
            var referenceModel = new ReferenceTypeModel
            {
                Name         = property.Name,
                DisplayName  = !string.IsNullOrEmpty(displayName) ? displayName : property.Name,
                Description  = property.GetDescription(),
                Role         = referenceAttr.Role,
                RelationType = referenceAttr.RelationType,
                IsRequired   = referenceAttr.IsRequired,
                IsCollection = typeof(IEnumerable <IResource>).IsAssignableFrom(property.PropertyType)
            };

            // Get type constraints
            Type targetType = property.PropertyType;

            if (referenceModel.IsCollection)
            {
                targetType = EntryConvert.ElementType(targetType);
            }
            var typeConstraints = MergeTypeConstraints(property, targetType, overrides);

            referenceModel.SupportedTypes = TypeController.SupportedTypes(typeConstraints).Select(t => t.Name).ToArray();

            return(referenceModel);
        }
示例#2
0
        /// <summary>
        /// Check if a property is a collection of primitives
        /// </summary>
        private static bool IsPrimitiveCollection(Type memberType)
        {
            if (!EntryConvert.IsCollection(memberType))
            {
                return(false);
            }

            var elementType = EntryConvert.ElementType(memberType);

            return(EntryConvert.ValueOrStringType(elementType));
        }
        /// <summary>
        /// Get all reference overrides from a resources properties
        /// </summary>
        private static Dictionary <string, List <Type> > GetReferenceOverrides(IEnumerable <PropertyInfo> properties)
        {
            // TODO Type wrappers in AL5
            var referenceOverrides = (from prop in properties
                                      let overrideAtt = prop.GetCustomAttribute <ReferenceOverrideAttribute>()
                                                        where overrideAtt != null
                                                        let targetType = typeof(IEnumerable <IResource>).IsAssignableFrom(prop.PropertyType)
                                        ? EntryConvert.ElementType(prop.PropertyType) : prop.PropertyType
                                                                         group targetType by overrideAtt.Source into g
                                                                         select new { g.Key, overrides = g.ToList() }).ToDictionary(v => v.Key, v => v.overrides);

            return(referenceOverrides);
        }