コード例 #1
0
        public void IsViewComponent_GenericViewComponent_ReturnsFalse()
        {
            // Arrange
            var testVisitor = new ViewComponentTypeVisitor(
                TestViewComponentAttributeSymbol,
                TestNonViewComponentAttributeSymbol,
                new List <INamedTypeSymbol>());
            var tagHelperSymbol = Compilation.GetTypeByMetadataName(typeof(Invalid_GenericViewComponent <>).FullName);

            // Act
            var isViewComponent = testVisitor.IsViewComponent(tagHelperSymbol);

            // Assert
            Assert.False(isViewComponent);
        }
コード例 #2
0
        public void IsViewComponent_DecoratedViewComponent_ReturnsTrue()
        {
            // Arrange
            var testVisitor = new ViewComponentTypeVisitor(
                TestViewComponentAttributeSymbol,
                TestNonViewComponentAttributeSymbol,
                new List <INamedTypeSymbol>());
            var tagHelperSymbol = Compilation.GetTypeByMetadataName(typeof(Valid_DecoratedVC).FullName);

            // Act
            var isViewComponent = testVisitor.IsViewComponent(tagHelperSymbol);

            // Assert
            Assert.True(isViewComponent);
        }
コード例 #3
0
        public void Execute(TagHelperDescriptorProviderContext context)
        {
            if (context == null)
            {
                throw new ArgumentNullException(nameof(context));
            }

            var compilation = context.GetCompilation();

            if (compilation == null)
            {
                // No compilation, nothing to do.
                return;
            }

            var types   = new List <INamedTypeSymbol>();
            var visitor = ViewComponentTypeVisitor.Create(compilation, types);

            if (ForceEnabled)
            {
                visitor.Enabled = true;
            }

            // We always visit the global namespace.
            visitor.Visit(compilation.Assembly.GlobalNamespace);

            foreach (var reference in compilation.References)
            {
                if (compilation.GetAssemblyOrModuleSymbol(reference) is IAssemblySymbol assembly)
                {
                    if (IsTagHelperAssembly(assembly))
                    {
                        visitor.Visit(assembly.GlobalNamespace);
                    }
                }
            }

            var factory = new ViewComponentTagHelperDescriptorFactory(compilation);

            for (var i = 0; i < types.Count; i++)
            {
                context.Results.Add(factory.CreateDescriptor(types[i]));
            }
        }
        public void Execute(TagHelperDescriptorProviderContext context)
        {
            if (context == null)
            {
                throw new ArgumentNullException(nameof(context));
            }

            var compilation = context.GetCompilation();

            if (compilation == null)
            {
                // No compilation, nothing to do.
                return;
            }

            var vcAttribute    = compilation.GetTypeByMetadataName(ViewComponentTypes.ViewComponentAttribute);
            var nonVCAttribute = compilation.GetTypeByMetadataName(ViewComponentTypes.NonViewComponentAttribute);

            if (vcAttribute == null || vcAttribute.TypeKind == TypeKind.Error)
            {
                // Could not find attributes we care about in the compilation. Nothing to do.
                return;
            }

            var types   = new List <INamedTypeSymbol>();
            var visitor = new ViewComponentTypeVisitor(vcAttribute, nonVCAttribute, types);

            var targetReference = context.Items.GetTargetMetadataReference();

            if (targetReference is not null)
            {
                if (compilation.GetAssemblyOrModuleSymbol(targetReference) is IAssemblySymbol targetAssembly && IsTagHelperAssembly(targetAssembly))
                {
                    visitor.Visit(targetAssembly.GlobalNamespace);
                }
            }
            else
            {
                visitor.Visit(compilation.Assembly.GlobalNamespace);
                foreach (var reference in compilation.References)
                {
                    if (compilation.GetAssemblyOrModuleSymbol(reference) is IAssemblySymbol assembly)
                    {
                        if (IsTagHelperAssembly(assembly))
                        {
                            visitor.Visit(assembly.GlobalNamespace);
                        }
                    }
                }
            }

            var factory = new ViewComponentTagHelperDescriptorFactory(compilation);

            for (var i = 0; i < types.Count; i++)
            {
                var descriptor = factory.CreateDescriptor(types[i]);

                if (descriptor != null)
                {
                    context.Results.Add(descriptor);
                }
            }
        }