Ejemplo n.º 1
0
        private static PropertyActivator <ViewContext> CreateActivateInfo(
            PropertyInfo property,
            PropertyValueAccessors valueAccessors)
        {
            Func <ViewContext, object> valueAccessor;

            if (typeof(ViewDataDictionary).IsAssignableFrom(property.PropertyType))
            {
                // Logic looks reversed in condition above but is OK. Support only properties of base
                // ViewDataDictionary type and activationInfo.ViewDataDictionaryType. VDD<AnotherType> will fail when
                // assigning to the property (InvalidCastException) and that's fine.
                valueAccessor = context => context.ViewData;
            }
            else if (property.PropertyType == typeof(IUrlHelper))
            {
                // W.r.t. specificity of above condition: Users are much more likely to inject their own
                // IUrlHelperFactory than to create a class implementing IUrlHelper (or a sub-interface) and inject
                // that. But the second scenario is supported. (Note the class must implement ICanHasViewContext.)
                valueAccessor = valueAccessors.UrlHelperAccessor;
            }
            else if (property.PropertyType == typeof(IJsonHelper))
            {
                valueAccessor = valueAccessors.JsonHelperAccessor;
            }
            else if (property.PropertyType == typeof(DiagnosticSource))
            {
                valueAccessor = valueAccessors.DiagnosticSourceAccessor;
            }
            else if (property.PropertyType == typeof(HtmlEncoder))
            {
                valueAccessor = valueAccessors.HtmlEncoderAccessor;
            }
            else if (property.PropertyType == typeof(IModelExpressionProvider))
            {
                valueAccessor = valueAccessors.ModelExpressionProviderAccessor;
            }
            else
            {
                valueAccessor = context =>
                {
                    var serviceProvider = context.HttpContext.RequestServices;
                    var value           = serviceProvider.GetRequiredService(property.PropertyType);
                    (value as IViewContextAware)?.Contextualize(context);

                    return(value);
                };
            }

            return(new PropertyActivator <ViewContext>(property, valueAccessor));
        }
Ejemplo n.º 2
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);
    }
Ejemplo n.º 3
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);
        }