예제 #1
0
        public void CreateBindingDetails_FindsCustomAttributes_OnParameter()
        {
            // Arrange
            var parameterAttributes = new object[]
            {
                new CustomAttribute {
                    Identifier = "Instance1"
                },
                new CustomAttribute {
                    Identifier = "Instance2"
                }
            };

            var context = new BindingMetadataProviderContext(
                ModelMetadataIdentity.ForParameter(ParameterInfos.SampleParameterInfo),
                new ModelAttributes(Array.Empty <object>(), null, parameterAttributes));

            var provider = new DefaultBindingMetadataProvider();

            // Act
            provider.CreateBindingMetadata(context);

            // Assert
            Assert.Collection(context.Attributes,
                              a => Assert.Equal("Instance1", ((CustomAttribute)a).Identifier),
                              a => Assert.Equal("Instance2", ((CustomAttribute)a).Identifier));
            Assert.Equal(2, context.ParameterAttributes.Count);
        }
예제 #2
0
        public void CreateBindingDetails_FindsBindRequired_OnParameter()
        {
            // Arrange
            var parameterAttributes = new object[]
            {
                new BindRequiredAttribute(),
            };

            var context = new BindingMetadataProviderContext(
                ModelMetadataIdentity.ForParameter(ParameterInfos.SampleParameterInfo),
                new ModelAttributes(Array.Empty <object>(), null, parameterAttributes));

            var provider = new DefaultBindingMetadataProvider();

            // Act
            provider.CreateBindingMetadata(context);

            // Assert
            Assert.True(context.BindingMetadata.IsBindingAllowed);
            Assert.True(context.BindingMetadata.IsBindingRequired);
        }
        private DefaultMetadataDetails CreateConstructorDetails(ModelMetadataIdentity constructorKey)
        {
            var constructor       = constructorKey.ConstructorInfo;
            var parameters        = constructor.GetParameters();
            var parameterMetadata = new ModelMetadata[parameters.Length];
            var parameterTypes    = new Type[parameters.Length];

            for (var i = 0; i < parameters.Length; i++)
            {
                var parameter        = parameters[i];
                var parameterDetails = CreateParameterDetails(ModelMetadataIdentity.ForParameter(parameter));
                parameterMetadata[i] = CreateModelMetadata(parameterDetails);

                parameterTypes[i] = parameter.ParameterType;
            }

            var constructorDetails = new DefaultMetadataDetails(constructorKey, ModelAttributes.Empty);

            constructorDetails.BoundConstructorParameters = parameterMetadata;
            constructorDetails.BoundConstructorInvoker    = CreateObjectFactory(constructor);

            return(constructorDetails);
 private ModelMetadataCacheEntry GetCacheEntry(ParameterInfo parameter)
 {
     return(_typeCache.GetOrAdd(
                ModelMetadataIdentity.ForParameter(parameter),
                _cacheEntryFactory));
 }