public void CreateBoundProperties_CopiesPropertyDescriptorsFromPagePropertyModel()
        {
            // Arrange
            var actionDescriptor = new PageActionDescriptor();
            var handlerTypeInfo  = typeof(HandlerWithProperty).GetTypeInfo();
            var propertyModel    = new PagePropertyModel(
                handlerTypeInfo.GetProperty(nameof(HandlerWithProperty.Property)),
                new object[0])
            {
                PropertyName = nameof(HandlerWithProperty.Property),
                BindingInfo  = new BindingInfo(),
            };
            var pageApplicationModel = new PageApplicationModel(actionDescriptor, handlerTypeInfo, new object[0])
            {
                HandlerProperties =
                {
                    propertyModel,
                }
            };

            // Act
            var propertyDescriptors = CompiledPageActionDescriptorBuilder.CreateBoundProperties(pageApplicationModel);

            // Assert
            Assert.Collection(
                propertyDescriptors,
                p =>
            {
                Assert.Same(propertyModel.PropertyName, p.Name);
                Assert.Same(typeof(int), p.ParameterType);
                Assert.Same(propertyModel.PropertyInfo, p.Property);
                Assert.Same(propertyModel.BindingInfo, p.BindingInfo);
            });
        }
Пример #2
0
        /// <summary>
        /// Creates a new instance of <see cref="PagePropertyModel"/> from a given <see cref="PagePropertyModel"/>.
        /// </summary>
        /// <param name="other">The <see cref="PagePropertyModel"/> which needs to be copied.</param>
        public PagePropertyModel(PagePropertyModel other)
            : base(other)
        {
            if (other == null)
            {
                throw new ArgumentNullException(nameof(other));
            }

            Page         = other.Page;
            BindingInfo  = BindingInfo == null ? null : new BindingInfo(other.BindingInfo);
            PropertyInfo = other.PropertyInfo;
        }
Пример #3
0
        /// <summary>
        /// Creates a new instance of <see cref="PagePropertyModel"/> from a given <see cref="PagePropertyModel"/>.
        /// </summary>
        /// <param name="other">The <see cref="PagePropertyModel"/> which needs to be copied.</param>
        public PagePropertyModel(PagePropertyModel other)
        {
            if (other == null)
            {
                throw new ArgumentNullException(nameof(other));
            }

            Page         = other.Page;
            Attributes   = new List <object>(other.Attributes);
            BindingInfo  = BindingInfo == null ? null : new BindingInfo(other.BindingInfo);
            PropertyInfo = other.PropertyInfo;
            PropertyName = other.PropertyName;
            Properties   = new Dictionary <object, object>(other.Properties);
        }
        /// <summary>
        /// Creates a <see cref="PagePropertyModel"/> for the <paramref name="property"/>.
        /// </summary>
        /// <param name="property">The <see cref="PropertyInfo"/>.</param>
        /// <returns>The <see cref="PagePropertyModel"/>.</returns>
        protected virtual PagePropertyModel CreatePropertyModel(PropertyInfo property)
        {
            if (property == null)
            {
                throw new ArgumentNullException(nameof(property));
            }

            var propertyAttributes = property.GetCustomAttributes(inherit: true);

            // BindingInfo for properties can be either specified by decorating the property with binding-specific attributes.
            // ModelMetadata also adds information from the property's type and any configured IBindingMetadataProvider.
            var propertyMetadata = _modelMetadataProvider.GetMetadataForProperty(property.DeclaringType, property.Name);
            var bindingInfo      = BindingInfo.GetBindingInfo(propertyAttributes, propertyMetadata);

            if (bindingInfo == null)
            {
                // Look for BindPropertiesAttribute on the handler type if no BindingInfo was inferred for the property.
                // This allows a user to enable model binding on properties by decorating the controller type with BindPropertiesAttribute.
                var declaringType           = property.DeclaringType;
                var bindPropertiesAttribute = declaringType.GetCustomAttribute <BindPropertiesAttribute>(inherit: true);
                if (bindPropertiesAttribute != null)
                {
                    var requestPredicate = bindPropertiesAttribute.SupportsGet ? _supportsAllRequests : _supportsNonGetRequests;
                    bindingInfo = new BindingInfo
                    {
                        RequestPredicate = requestPredicate,
                    };
                }
            }

            var model = new PagePropertyModel(property, propertyAttributes)
            {
                PropertyName = property.Name,
                BindingInfo  = bindingInfo,
            };

            return(model);
        }