/// <summary>
 /// Instantiates a new <see cref="DefaultTagHelperActivator"/> instance.
 /// </summary>
 public DefaultTagHelperActivator()
 {
     _injectActions           = new ConcurrentDictionary <Type, PropertyActivator <ViewContext>[]>();
     _getPropertiesToActivate = type =>
                                PropertyActivator <ViewContext> .GetPropertiesToActivate(
         type, typeof(ActivateAttribute), CreateActivateInfo);
 }
示例#2
0
        public Func <PageContext, object>?CreateModelFactory(CompiledPageActionDescriptor descriptor)
        {
            if (descriptor == null)
            {
                throw new ArgumentNullException(nameof(descriptor));
            }

            if (descriptor.ModelTypeInfo == null)
            {
                return(null);
            }

            var modelActivator    = _modelActivator.CreateActivator(descriptor);
            var propertyActivator = PropertyActivator <PageContext> .GetPropertiesToActivate(
                descriptor.ModelTypeInfo.AsType(),
                typeof(PageContextAttribute),
                _createActivateInfo,
                includeNonPublic : false);

            return(pageContext =>
            {
                var model = modelActivator(pageContext);
                for (var i = 0; i < propertyActivator.Length; i++)
                {
                    propertyActivator[i].Activate(model, pageContext);
                }

                return model;
            });
        }
示例#3
0
        private PageActivationInfo CreateViewActivationInfo(Type type)
        {
            // Look for a property named "Model". If it is non-null, we'll assume this is
            // the equivalent of TModel Model property on RazorPage<TModel>
            var modelProperty = type.GetRuntimeProperty(ModelPropertyName);

            if (modelProperty == null)
            {
                var message = Resources.FormatViewCannotBeActivated(type.FullName, GetType().FullName);
                throw new InvalidOperationException(message);
            }

            var modelType    = modelProperty.PropertyType;
            var viewDataType = typeof(ViewDataDictionary <>).MakeGenericType(modelType);

            return(new PageActivationInfo
            {
                ViewDataDictionaryType = viewDataType,
                PropertyActivators = PropertyActivator <ViewContext> .GetPropertiesToActivate(
                    type,
                    typeof(RazorInjectAttribute),
                    CreateActivateInfo,
                    includeNonPublic : true)
            });
        }
        public ViewComponentPropertyActivator()
        {
            this.getPropertiesToActivate = type => PropertyActivator <ViewComponentContext>
                                           .GetPropertiesToActivate(type, typeof(ViewComponentContextAttribute), CreateActivateInfo);

            this.propertyActivatorCache = new ConcurrentDictionary <Type, PropertyActivator <ViewComponentContext>[]>();
        }
示例#5
0
 private static PropertyActivator <ViewContext>[] GetPropertiesToActivate(Type type)
 {
     return(PropertyActivator <ViewContext> .GetPropertiesToActivate(
                type,
                typeof(ViewContextAttribute),
                _createActivateInfo));
 }
 /// <summary>
 /// Initializes a new instance of <see cref="DefaultViewComponentActivator"/> class.
 /// </summary>
 public DefaultViewComponentActivator()
 {
     _valueAccessorLookup     = CreateValueAccessorLookup();
     _injectActions           = new ConcurrentDictionary <Type, PropertyActivator <ViewContext>[]>();
     _getPropertiesToActivate = type =>
                                PropertyActivator <ViewContext> .GetPropertiesToActivate(
         type, typeof(ActivateAttribute), CreateActivateInfo);
 }
    private PropertyActivator <ControllerContext>[] GetPropertiesToActivate(Type type)
    {
        var activators = PropertyActivator <ControllerContext> .GetPropertiesToActivate(
            type,
            typeof(ViewDataDictionaryAttribute),
            p => new PropertyActivator <ControllerContext>(p, GetViewDataDictionary));

        return(activators);
    }
示例#8
0
    public void Activate_SetsPropertyValue()
    {
        // Arrange
        var instance  = new TestClass();
        var typeInfo  = instance.GetType().GetTypeInfo();
        var property  = typeInfo.GetDeclaredProperty("IntProperty");
        var activator = new PropertyActivator <int>(property, valueAccessor: (val) => val + 1);

        // Act
        activator.Activate(instance, 123);

        // Assert
        Assert.Equal(124, instance.IntProperty);
    }
        public void Activate_SetsPropertyValue()
        {
            // Arrange
            var instance = new TestClass();
            var typeInfo = instance.GetType().GetTypeInfo();
            var property = typeInfo.GetDeclaredProperty("IntProperty");
            var activator = new PropertyActivator<int>(property, valueAccessor: (val) => val + 1);

            // Act
            activator.Activate(instance, 123);

            // Assert
            Assert.Equal(124, instance.IntProperty);
        }
        /// <summary>
        /// Initializes a new <see cref="DefaultTagHelperFactory"/> instance.
        /// </summary>
        /// <param name="activator">
        /// The <see cref="ITagHelperActivator"/> used to create tag helper instances.
        /// </param>
        public DefaultTagHelperFactory(ITagHelperActivator activator)
        {
            if (activator == null)
            {
                throw new ArgumentNullException(nameof(activator));
            }

            _activator               = activator;
            _injectActions           = new ConcurrentDictionary <Type, PropertyActivator <ViewContext>[]>();
            _getPropertiesToActivate = type =>
                                       PropertyActivator <ViewContext> .GetPropertiesToActivate(
                type,
                typeof(ViewContextAttribute),
                _createActivateInfo);
        }
示例#11
0
        private void InjectProperties(ViewComponentContext context, object viewComponent)
        {
            var propertiesToActivate = _injectActions.GetOrAdd(
                viewComponent.GetType(),
                type =>
                PropertyActivator <ViewComponentContext> .GetPropertiesToActivate(
                    type,
                    typeof(ViewComponentContextAttribute),
                    CreateActivateInfo));

            for (var i = 0; i < propertiesToActivate.Length; i++)
            {
                var activateInfo = propertiesToActivate[i];
                activateInfo.Activate(viewComponent, context);
            }
        }
示例#12
0
        private static PropertyActivator <ControllerContext>[] GetPropertiesToActivate(Type type)
        {
            IEnumerable <PropertyActivator <ControllerContext> > activators;

            activators = PropertyActivator <ControllerContext> .GetPropertiesToActivate(
                type,
                typeof(ActionContextAttribute),
                p => new PropertyActivator <ControllerContext>(p, c => c));

            activators = activators.Concat(PropertyActivator <ControllerContext> .GetPropertiesToActivate(
                                               type,
                                               typeof(ControllerContextAttribute),
                                               p => new PropertyActivator <ControllerContext>(p, c => c)));

            return(activators.ToArray());
        }
示例#13
0
    public void GetPropertiesToActivate_IncludesNonPublic()
    {
        // Arrange
        var instance = new TestClassWithPropertyVisiblity();
        var typeInfo = instance.GetType().GetTypeInfo();

        // Act
        var propertiesToActivate = PropertyActivator <int> .GetPropertiesToActivate(
            typeof(TestClassWithPropertyVisiblity),
            typeof(TestActivateAttribute),
            (propertyInfo) => new PropertyActivator <int>(propertyInfo, valueAccessor : (val) => val),
            includeNonPublic : true);

        // Assert
        Assert.Equal(5, propertiesToActivate.Length);
    }
示例#14
0
        private PropertyActivator <Contexts>[] GetPropertiesToActivate(Type type)
        {
            IEnumerable <PropertyActivator <Contexts> > activators;

            activators = PropertyActivator <Contexts> .GetPropertiesToActivate(
                type,
                typeof(ActionContextAttribute),
                p => new PropertyActivator <Contexts>(p, c => c.ActionContext));

            activators = activators.Concat(PropertyActivator <Contexts> .GetPropertiesToActivate(
                                               type,
                                               typeof(ActionBindingContextAttribute),
                                               p => new PropertyActivator <Contexts>(p, c => c.ActionBindingContext)));

            return(activators.ToArray());
        }
示例#15
0
    public void GetPropertiesToActivate_ExcludesNonPublic()
    {
        // Arrange
        var instance             = new TestClassWithPropertyVisiblity();
        var typeInfo             = instance.GetType().GetTypeInfo();
        var expectedPropertyInfo = typeInfo.GetDeclaredProperty("Public");

        // Act
        var propertiesToActivate = PropertyActivator <int> .GetPropertiesToActivate(
            typeof(TestClassWithPropertyVisiblity),
            typeof(TestActivateAttribute),
            (propertyInfo) => new PropertyActivator <int>(propertyInfo, valueAccessor : (val) => val));

        // Assert
        Assert.Single(propertiesToActivate);
        Assert.Single(propertiesToActivate, p => p.PropertyInfo == expectedPropertyInfo);
    }
示例#16
0
    /// <inheritdoc />
    public override void Init(TagHelperContext context)
    {
        if (PropertyActivator == null)
        {
            var serviceProvider = ViewContext.HttpContext.RequestServices;
            PropertyActivator = serviceProvider.GetRequiredService <ITagHelperComponentPropertyActivator>();
        }

        foreach (var component in _components)
        {
            PropertyActivator.Activate(ViewContext, component);
            component.Init(context);
            if (_logger.IsEnabled(LogLevel.Debug))
            {
                _logger.TagHelperComponentInitialized(component.GetType().FullName !);
            }
        }
    }
示例#17
0
    public void Activate_InvokesValueAccessorWithExpectedValue()
    {
        // Arrange
        var instance    = new TestClass();
        var typeInfo    = instance.GetType().GetTypeInfo();
        var property    = typeInfo.GetDeclaredProperty("IntProperty");
        var invokedWith = -1;
        var activator   = new PropertyActivator <int>(
            property,
            valueAccessor: (val) =>
        {
            invokedWith = val;
            return(val);
        });

        // Act
        activator.Activate(instance, 123);

        // Assert
        Assert.Equal(123, invokedWith);
    }
示例#18
0
        public void Activate_InvokesValueAccessorWithExpectedValue()
        {
            // Arrange
            var instance = new TestClass();
            var typeInfo = instance.GetType().GetTypeInfo();
            var property = typeInfo.GetDeclaredProperty("IntProperty");
            var invokedWith = -1;
            var activator = new PropertyActivator<int>(
                property,
                valueAccessor: (val) =>
                {
                    invokedWith = val;
                    return val;
                });

            // Act
            activator.Activate(instance, 123);

            // Assert
            Assert.Equal(123, invokedWith);
        }
示例#19
0
    public RazorPagePropertyActivator(
        Type pageType,
        Type?declaredModelType,
        IModelMetadataProvider metadataProvider,
        PropertyValueAccessors propertyValueAccessors)
    {
        _metadataProvider = metadataProvider;

        // In the absence of a model on the current type, we'll attempt to use ViewDataDictionary<object> on the current type.
        var viewDataDictionaryModelType = declaredModelType ?? typeof(object);

        _viewDataDictionaryType = typeof(ViewDataDictionary <>).MakeGenericType(viewDataDictionaryModelType);
        _rootFactory            = ViewDataDictionaryFactory.CreateFactory(viewDataDictionaryModelType);
        _nestedFactory          = ViewDataDictionaryFactory.CreateNestedFactory(viewDataDictionaryModelType);

        _propertyActivators = PropertyActivator <ViewContext> .GetPropertiesToActivate(
            pageType,
            typeof(RazorInjectAttribute),
            propertyInfo => CreateActivateInfo(propertyInfo, propertyValueAccessors),
            includeNonPublic : true);
    }
示例#20
0
    public void GetPropertiesToActivate_CanCreateCustomPropertyActivators()
    {
        // Arrange
        var instance             = new TestClass();
        var typeInfo             = instance.GetType().GetTypeInfo();
        var expectedPropertyInfo = typeInfo.GetDeclaredProperty("IntProperty");

        // Act
        var propertiesToActivate = PropertyActivator <int> .GetPropertiesToActivate(
            type : typeof(TestClass),
            activateAttributeType : typeof(TestActivateAttribute),
            createActivateInfo :
            (propertyInfo) => new PropertyActivator <int>(expectedPropertyInfo, valueAccessor: (val) => val + 1));

        // Assert
        Assert.Collection(
            propertiesToActivate,
            (activator) =>
        {
            Assert.Equal(expectedPropertyInfo, activator.PropertyInfo);
        });
    }
示例#21
0
        public RazorPagePropertyActivator(
            Type pageType,
            Type modelType,
            IModelMetadataProvider metadataProvider,
            PropertyValueAccessors propertyValueAccessors)
        {
            _metadataProvider = metadataProvider;


            if (modelType != null)
            {
                _viewDataDictionaryType = typeof(ViewDataDictionary <>).MakeGenericType(modelType);
                _rootFactory            = ViewDataDictionaryFactory.CreateFactory(modelType.GetTypeInfo());
                _nestedFactory          = ViewDataDictionaryFactory.CreateNestedFactory(modelType.GetTypeInfo());
            }

            _propertyActivators = PropertyActivator <ViewContext> .GetPropertiesToActivate(
                pageType,
                typeof(RazorInjectAttribute),
                propertyInfo => CreateActivateInfo(propertyInfo, propertyValueAccessors),
                includeNonPublic : true);
        }
示例#22
0
 public PropertyHolderN(PropertyActivator activator = null) : base(activator)
 {
     ClassHelper = new NotifyClassHelper(this);
 }