示例#1
0
        /// <summary>
        /// Creates and activates a <see cref="ITagHelper"/>.
        /// </summary>
        /// <typeparam name="TTagHelper">A <see cref="ITagHelper"/> type.</typeparam>
        /// <returns>The activated <see cref="ITagHelper"/>.</returns>
        /// <remarks>
        /// <typeparamref name="TTagHelper"/> must have a parameterless constructor.
        /// </remarks>
        public TTagHelper CreateTagHelper <TTagHelper>() where TTagHelper : ITagHelper
        {
            var tagHelper = TypeActivatorCache.CreateInstance <TTagHelper>(
                ViewContext.HttpContext.RequestServices,
                typeof(TTagHelper));

            TagHelperActivator.Activate(tagHelper, ViewContext);

            return(tagHelper);
        }
        private static TestRazorPage CreateTestRazorPage()
        {
            var modelMetadataProvider   = new EmptyModelMetadataProvider();
            var modelExpressionProvider = new ModelExpressionProvider(modelMetadataProvider, new ExpressionTextCache());
            var activator = new RazorPageActivator(
                modelMetadataProvider,
                new UrlHelperFactory(),
                new JsonHelper(
                    new JsonOutputFormatter(new JsonSerializerSettings(), ArrayPool <char> .Shared),
                    ArrayPool <char> .Shared),
                new DiagnosticListener("Microsoft.AspNetCore"),
                new HtmlTestEncoder(),
                modelExpressionProvider);

            var serviceProvider    = new Mock <IServiceProvider>();
            var typeActivator      = new TypeActivatorCache();
            var tagHelperActivator = new DefaultTagHelperActivator(typeActivator);
            var myService          = new MyService();

            serviceProvider.Setup(mock => mock.GetService(typeof(MyService)))
            .Returns(myService);
            serviceProvider.Setup(mock => mock.GetService(typeof(ITagHelperFactory)))
            .Returns(new DefaultTagHelperFactory(tagHelperActivator));
            serviceProvider.Setup(mock => mock.GetService(typeof(ITagHelperActivator)))
            .Returns(tagHelperActivator);
            serviceProvider.Setup(mock => mock.GetService(typeof(ITypeActivatorCache)))
            .Returns(typeActivator);
            serviceProvider.Setup(mock => mock.GetService(It.Is <Type>(serviceType =>
                                                                       serviceType.GetTypeInfo().IsGenericType&& serviceType.GetGenericTypeDefinition() == typeof(IEnumerable <>))))
            .Returns <Type>(serviceType =>
            {
                var enumerableType = serviceType.GetGenericArguments().First();
                return(typeof(Enumerable).GetMethod("Empty").MakeGenericMethod(enumerableType).Invoke(null, null));
            });
            var httpContext = new Mock <HttpContext>();

            httpContext.SetupGet(c => c.RequestServices)
            .Returns(serviceProvider.Object);

            var actionContext = new ActionContext(httpContext.Object, new RouteData(), new ActionDescriptor());
            var viewData      = new ViewDataDictionary(new EmptyModelMetadataProvider(), new ModelStateDictionary());
            var viewContext   = new ViewContext(
                actionContext,
                Mock.Of <IView>(),
                viewData,
                Mock.Of <ITempDataDictionary>(),
                TextWriter.Null,
                new HtmlHelperOptions());

            return(new TestRazorPage
            {
                ViewContext = viewContext
            });
        }
        private static TestRazorPage CreateTestRazorPage()
        {
            var activator          = new RazorPageActivator(new EmptyModelMetadataProvider());
            var serviceProvider    = new Mock <IServiceProvider>();
            var typeActivator      = new TypeActivatorCache();
            var tagHelperActivator = new DefaultTagHelperActivator(typeActivator);
            var myService          = new MyService();

            serviceProvider.Setup(mock => mock.GetService(typeof(MyService)))
            .Returns(myService);
            serviceProvider.Setup(mock => mock.GetService(typeof(ITagHelperFactory)))
            .Returns(new DefaultTagHelperFactory(tagHelperActivator));
            serviceProvider.Setup(mock => mock.GetService(typeof(ITagHelperActivator)))
            .Returns(tagHelperActivator);
            serviceProvider.Setup(mock => mock.GetService(typeof(ITypeActivatorCache)))
            .Returns(typeActivator);
            serviceProvider.Setup(mock => mock.GetService(It.Is <Type>(serviceType =>
                                                                       serviceType.GetTypeInfo().IsGenericType&& serviceType.GetGenericTypeDefinition() == typeof(IEnumerable <>))))
            .Returns <Type>(serviceType =>
            {
                var enumerableType = serviceType.GetGenericArguments().First();
                return(typeof(Enumerable).GetMethod("Empty").MakeGenericMethod(enumerableType).Invoke(null, null));
            });
            var httpContext = new Mock <HttpContext>();

            httpContext.SetupGet(c => c.RequestServices)
            .Returns(serviceProvider.Object);

            var actionContext = new ActionContext(httpContext.Object, new RouteData(), new ActionDescriptor());
            var viewData      = new ViewDataDictionary(new EmptyModelMetadataProvider());
            var viewContext   = new ViewContext(
                actionContext,
                Mock.Of <IView>(),
                viewData,
                Mock.Of <ITempDataDictionary>(),
                TextWriter.Null,
                new HtmlHelperOptions());

            return(new TestRazorPage
            {
                ViewContext = viewContext
            });
        }
示例#4
0
 public ControllerActivator(IConfigurationRoot configuration)
 {
     typeActivatorCache = new TypeActivatorCache();
     defaultActivator   = new DefaultControllerActivator(typeActivatorCache);
     this.configuration = configuration;
 }
        /// <summary>
        /// Resolves a type using the type activator cache, just as ASP.NET Core does
        /// </summary>
        /// <typeparam name="T">The type to resolve</typeparam>
        /// <returns>The resolved type, or null if cannot be resolved</returns>
        protected T ResolveType <T>(ServiceProvider serviceProvider)
        {
            TypeActivatorCache typeActivatorCache = new TypeActivatorCache();

            return(typeActivatorCache.CreateInstance <T>(serviceProvider, typeof(T)));
        }
 public ControllerActivator(IConfigurationRoot config)
 {
     Configuration      = config;
     TypeActivatorCache = new TypeActivatorCache();
     DefaultActivator   = new DefaultControllerActivator(TypeActivatorCache);
 }