private static void AddProperty(StructuralTypeConfiguration config, string propertyName)
    {
        var propertyInfo = config.ClrType.GetProperty(propertyName);

        if (propertyInfo is null)
        {
            throw new MissingMemberException($"Unable to find the property '{propertyName}'.", propertyName);
        }

        var property = config.AddProperty(propertyInfo);

        var attribute = propertyInfo.GetCustomAttribute <DataMemberAttribute>(inherit: false);

        if (attribute is not null && !String.IsNullOrWhiteSpace(attribute.Name))
        {
            property.Name = attribute.Name;
        }
示例#2
0
        private static void AddProperty(StructuralTypeConfiguration config, string propertyName)
        {
            var propertyInfo = config.ClrType.GetProperty(propertyName);
            var property     = config.AddProperty(propertyInfo);

            var attribute = propertyInfo.GetCustomAttribute <DataMemberAttribute>(inherit: false);

            if (attribute != null && !String.IsNullOrWhiteSpace(attribute.Name))
            {
                property.Name = attribute.Name;
            }
            else
            {
                var caser = new LowerCamelCaser();
                property.Name = caser.ToLowerCamelCase(propertyInfo.Name);
            }
        }
        private void MapStructuralProperty(StructuralTypeConfiguration type, PropertyInfo property, PropertyKind propertyKind, bool isCollection)
        {
            Contract.Assert(type != null);
            Contract.Assert(property != null);
            Contract.Assert(propertyKind == PropertyKind.Complex || propertyKind == PropertyKind.Primitive || propertyKind == PropertyKind.Enum);

            bool addedExplicitly = type.Properties.Any(p => p.PropertyInfo.Name == property.Name);

            PropertyConfiguration addedEdmProperty;

            if (!isCollection)
            {
                if (propertyKind == PropertyKind.Primitive)
                {
                    addedEdmProperty = type.AddProperty(property);
                }
                else if (propertyKind == PropertyKind.Enum)
                {
                    this.AddEnumType(TypeHelper.GetUnderlyingTypeOrSelf(property.PropertyType));
                    addedEdmProperty = type.AddEnumProperty(property);
                }
                else
                {
                    addedEdmProperty = type.AddComplexProperty(property);
                }
            }
            else
            {
                if (this._isQueryCompositionMode)
                {
                    Contract.Assert(propertyKind != PropertyKind.Complex, "we don't create complex types in query composition mode.");
                }

                if (property.PropertyType.IsGenericType)
                {
                    Type elementType = property.PropertyType.GetGenericArguments().First();
                    Type elementUnderlyingTypeOrSelf = TypeHelper.GetUnderlyingTypeOrSelf(elementType);

                    if (elementUnderlyingTypeOrSelf.IsEnum)
                    {
                        this.AddEnumType(elementUnderlyingTypeOrSelf);
                    }
                }
                else
                {
                    Type elementType;
                    if (property.PropertyType.IsCollection(out elementType))
                    {
                        Type elementUnderlyingTypeOrSelf = TypeHelper.GetUnderlyingTypeOrSelf(elementType);
                        if (elementUnderlyingTypeOrSelf.IsEnum)
                        {
                            this.AddEnumType(elementUnderlyingTypeOrSelf);
                        }
                    }
                }

                addedEdmProperty = type.AddCollectionProperty(property);
            }

            addedEdmProperty.AddedExplicitly = addedExplicitly;
        }