diff --git a/src/Compiler/Microsoft.AspNetCore.Razor.Language/src/Components/ComponentsApi.cs b/src/Compiler/Microsoft.AspNetCore.Razor.Language/src/Components/ComponentsApi.cs index 4578e1b42d6..b4ac1f9c2d4 100644 --- a/src/Compiler/Microsoft.AspNetCore.Razor.Language/src/Components/ComponentsApi.cs +++ b/src/Compiler/Microsoft.AspNetCore.Razor.Language/src/Components/ComponentsApi.cs @@ -1,8 +1,6 @@ // Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the MIT license. -#nullable disable - namespace Microsoft.AspNetCore.Razor.Language.Components; // Constants for method names used in code-generation @@ -40,9 +38,8 @@ public static class InjectAttribute public static class IComponent { - public const string FullTypeName = "Microsoft.AspNetCore.Components.IComponent"; - - public const string MetadataName = FullTypeName; + public const string MetadataName = WellKnownTypeNames.MicrosoftAspNetCoreComponentsIComponent; + public const string FullTypeName = MetadataName; } public static class IDictionary diff --git a/src/Compiler/Microsoft.CodeAnalysis.Razor/src/WellKnownTypeNames.cs b/src/Compiler/Microsoft.AspNetCore.Razor.Language/src/WellKnownTypeNames.cs similarity index 53% rename from src/Compiler/Microsoft.CodeAnalysis.Razor/src/WellKnownTypeNames.cs rename to src/Compiler/Microsoft.AspNetCore.Razor.Language/src/WellKnownTypeNames.cs index 39300933cb7..194c4ccd806 100644 --- a/src/Compiler/Microsoft.CodeAnalysis.Razor/src/WellKnownTypeNames.cs +++ b/src/Compiler/Microsoft.AspNetCore.Razor.Language/src/WellKnownTypeNames.cs @@ -1,9 +1,10 @@ // Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the MIT license. -namespace Microsoft.CodeAnalysis.Razor; +namespace Microsoft.AspNetCore.Razor.Language; -internal static class WellKnownTypeNames +public static class WellKnownTypeNames { + public const string MicrosoftAspNetCoreComponentsIComponent = "Microsoft.AspNetCore.Components.IComponent"; public const string SystemThreadingTasksTask1 = "System.Threading.Tasks.Task`1"; } diff --git a/src/Compiler/Microsoft.CodeAnalysis.Razor/src/ComponentDetectionConventions.cs b/src/Compiler/Microsoft.CodeAnalysis.Razor/src/ComponentDetectionConventions.cs index 0d633ae690b..c5f1d229273 100644 --- a/src/Compiler/Microsoft.CodeAnalysis.Razor/src/ComponentDetectionConventions.cs +++ b/src/Compiler/Microsoft.CodeAnalysis.Razor/src/ComponentDetectionConventions.cs @@ -1,10 +1,7 @@ // Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the MIT license. -#nullable enable - using System; -using System.Linq; namespace Microsoft.CodeAnalysis.Razor; @@ -25,24 +22,6 @@ public static bool IsComponent(INamedTypeSymbol symbol, INamedTypeSymbol icompon return symbol.DeclaredAccessibility == Accessibility.Public && !symbol.IsAbstract && - symbol.AllInterfaces.Contains(icomponentSymbol); - } - - public static bool IsComponent(INamedTypeSymbol symbol, string icomponentSymbolName) - { - if (symbol is null) - { - throw new ArgumentNullException(nameof(symbol)); - } - - if (icomponentSymbolName is null) - { - throw new ArgumentNullException(nameof(icomponentSymbolName)); - } - - return - symbol.DeclaredAccessibility == Accessibility.Public && - !symbol.IsAbstract && - symbol.AllInterfaces.Any(s => s.HasFullName(icomponentSymbolName)); + symbol.AllInterfaces.Contains(icomponentSymbol, SymbolEqualityComparer.Default); } } diff --git a/src/Compiler/Microsoft.CodeAnalysis.Razor/src/ComponentTagHelperDescriptorProvider.cs b/src/Compiler/Microsoft.CodeAnalysis.Razor/src/ComponentTagHelperDescriptorProvider.cs index 8932cd22a08..80b113571f3 100644 --- a/src/Compiler/Microsoft.CodeAnalysis.Razor/src/ComponentTagHelperDescriptorProvider.cs +++ b/src/Compiler/Microsoft.CodeAnalysis.Razor/src/ComponentTagHelperDescriptorProvider.cs @@ -30,15 +30,16 @@ public void Execute(TagHelperDescriptorProviderContext context) throw new ArgumentNullException(nameof(context)); } - var compilation = context.GetCompilation(); - if (compilation == null) + var typeProvider = context.GetTypeProvider(); + if (typeProvider == null + || !typeProvider.TryGetOrCreateTypeByMetadataName(WellKnownTypeNames.MicrosoftAspNetCoreComponentsIComponent, out var icomponentSymbol)) { // No compilation, nothing to do. return; } var types = new List(); - var visitor = new ComponentTypeVisitor(types); + var visitor = new ComponentTypeVisitor(types, icomponentSymbol); var targetSymbol = context.Items.GetTargetSymbol(); if (targetSymbol is not null) @@ -47,6 +48,7 @@ public void Execute(TagHelperDescriptorProviderContext context) } else { + var compilation = typeProvider.Compilation; visitor.Visit(compilation.Assembly.GlobalNamespace); foreach (var reference in compilation.References) { @@ -575,15 +577,17 @@ private enum PropertyKind private class ComponentTypeVisitor : SymbolVisitor { private readonly List _results; + private readonly INamedTypeSymbol _icomponentSymbol; - public ComponentTypeVisitor(List results) + public ComponentTypeVisitor(List results, INamedTypeSymbol icomponentSymbol) { _results = results; + _icomponentSymbol = icomponentSymbol; } public override void VisitNamedType(INamedTypeSymbol symbol) { - if (ComponentDetectionConventions.IsComponent(symbol, ComponentsApi.IComponent.MetadataName)) + if (ComponentDetectionConventions.IsComponent(symbol, _icomponentSymbol)) { _results.Add(symbol); } diff --git a/src/Compiler/Microsoft.CodeAnalysis.Razor/src/ImmutableArrayExtensions.cs b/src/Compiler/Microsoft.CodeAnalysis.Razor/src/ImmutableArrayExtensions.cs new file mode 100644 index 00000000000..d4367091e0c --- /dev/null +++ b/src/Compiler/Microsoft.CodeAnalysis.Razor/src/ImmutableArrayExtensions.cs @@ -0,0 +1,24 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. + +using System.Collections.Generic; +using System.Collections.Immutable; + +namespace Microsoft.CodeAnalysis.Razor; + +internal static class ImmutableArrayExtensions +{ + public static bool Contains(this ImmutableArray array, T item, TComparer comparer) + where TComparer : IEqualityComparer + { + foreach (var actual in array) + { + if (comparer.Equals(actual, item)) + { + return true; + } + } + + return false; + } +} diff --git a/src/Compiler/Microsoft.CodeAnalysis.Razor/src/WellKnownTypeProvider.cs b/src/Compiler/Microsoft.CodeAnalysis.Razor/src/WellKnownTypeProvider.cs index e3d7fa9feba..c9d7f8af09f 100644 --- a/src/Compiler/Microsoft.CodeAnalysis.Razor/src/WellKnownTypeProvider.cs +++ b/src/Compiler/Microsoft.CodeAnalysis.Razor/src/WellKnownTypeProvider.cs @@ -10,6 +10,7 @@ using System.Globalization; using System.Linq; using System.Threading; +using Microsoft.AspNetCore.Razor.Language; namespace Microsoft.CodeAnalysis.Razor;