Skip to content

Commit 13bc23b

Browse files
authored
Fix some trim warnings in Components.Endpoints (#49890)
- Add DynamicallyAccessedMembers attributes to places that flow Component Type around.
1 parent 28e5590 commit 13bc23b

18 files changed

+72
-26
lines changed

src/Components/Endpoints/src/Builder/ComponentTypeMetadata.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
// Licensed to the .NET Foundation under one or more agreements.
22
// The .NET Foundation licenses this file to you under the MIT license.
33

4+
using System.Diagnostics.CodeAnalysis;
5+
using static Microsoft.AspNetCore.Internal.LinkerFlags;
6+
47
namespace Microsoft.AspNetCore.Components.Endpoints;
58

69
/// <summary>
@@ -12,13 +15,14 @@ public class ComponentTypeMetadata
1215
/// Initializes a new instance of <see cref="ComponentTypeMetadata"/>.
1316
/// </summary>
1417
/// <param name="componentType">The component type.</param>
15-
public ComponentTypeMetadata(Type componentType)
18+
public ComponentTypeMetadata([DynamicallyAccessedMembers(Component)] Type componentType)
1619
{
1720
Type = componentType;
1821
}
1922

2023
/// <summary>
2124
/// Gets the component type.
2225
/// </summary>
26+
[DynamicallyAccessedMembers(Component)]
2327
public Type Type { get; }
2428
}

src/Components/Endpoints/src/Builder/RazorComponentEndpointDataSource.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,18 @@
22
// The .NET Foundation licenses this file to you under the MIT license.
33

44
using System.Diagnostics;
5+
using System.Diagnostics.CodeAnalysis;
56
using System.Linq;
67
using Microsoft.AspNetCore.Builder;
78
using Microsoft.AspNetCore.Components.Discovery;
89
using Microsoft.AspNetCore.Http;
910
using Microsoft.AspNetCore.Routing;
1011
using Microsoft.Extensions.Primitives;
12+
using static Microsoft.AspNetCore.Internal.LinkerFlags;
1113

1214
namespace Microsoft.AspNetCore.Components.Endpoints;
1315

14-
internal class RazorComponentEndpointDataSource<TRootComponent> : EndpointDataSource
16+
internal class RazorComponentEndpointDataSource<[DynamicallyAccessedMembers(Component)] TRootComponent> : EndpointDataSource
1517
{
1618
private readonly object _lock = new();
1719
private readonly List<Action<EndpointBuilder>> _conventions = new();

src/Components/Endpoints/src/Builder/RazorComponentEndpointDataSourceFactory.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
// Licensed to the .NET Foundation under one or more agreements.
22
// The .NET Foundation licenses this file to you under the MIT license.
33

4+
using System.Diagnostics.CodeAnalysis;
45
using Microsoft.AspNetCore.Components.Discovery;
56
using Microsoft.AspNetCore.Components.Endpoints;
67
using Microsoft.AspNetCore.Routing;
8+
using static Microsoft.AspNetCore.Internal.LinkerFlags;
79

810
namespace Microsoft.AspNetCore.Components.Infrastructure;
911

@@ -20,7 +22,7 @@ public RazorComponentEndpointDataSourceFactory(
2022
_providers = providers;
2123
}
2224

23-
public RazorComponentEndpointDataSource<TRootComponent> CreateDataSource<TRootComponent>(IEndpointRouteBuilder endpoints)
25+
public RazorComponentEndpointDataSource<TRootComponent> CreateDataSource<[DynamicallyAccessedMembers(Component)] TRootComponent>(IEndpointRouteBuilder endpoints)
2426
{
2527
var builder = ComponentApplicationBuilder.GetBuilder<TRootComponent>() ??
2628
DefaultRazorComponentApplication<TRootComponent>.Instance.GetBuilder();

src/Components/Endpoints/src/Builder/RazorComponentEndpointFactory.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
// Licensed to the .NET Foundation under one or more agreements.
22
// The .NET Foundation licenses this file to you under the MIT license.
33

4+
using System.Diagnostics.CodeAnalysis;
45
using Microsoft.AspNetCore.Antiforgery;
56
using Microsoft.AspNetCore.Builder;
67
using Microsoft.AspNetCore.Components.Discovery;
78
using Microsoft.AspNetCore.Http;
89
using Microsoft.AspNetCore.Routing;
910
using Microsoft.AspNetCore.Routing.Patterns;
1011
using Microsoft.Extensions.DependencyInjection;
12+
using static Microsoft.AspNetCore.Internal.LinkerFlags;
1113

1214
namespace Microsoft.AspNetCore.Components.Endpoints;
1315

@@ -19,7 +21,7 @@ internal class RazorComponentEndpointFactory
1921
internal void AddEndpoints(
2022
#pragma warning restore CA1822 // It's a singleton
2123
List<Endpoint> endpoints,
22-
Type rootComponent,
24+
[DynamicallyAccessedMembers(Component)] Type rootComponent,
2325
PageComponentInfo pageDefinition,
2426
IReadOnlyList<Action<EndpointBuilder>> conventions,
2527
IReadOnlyList<Action<EndpointBuilder>> finallyConventions)

src/Components/Endpoints/src/Builder/RazorComponentsEndpointRouteBuilderExtensions.cs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// Licensed to the .NET Foundation under one or more agreements.
22
// The .NET Foundation licenses this file to you under the MIT license.
33

4+
using System.Diagnostics.CodeAnalysis;
45
using System.Linq;
56
using Microsoft.AspNetCore.Components.Endpoints;
67
using Microsoft.AspNetCore.Components.Infrastructure;
@@ -9,20 +10,21 @@
910
using Microsoft.AspNetCore.StaticFiles;
1011
using Microsoft.Extensions.DependencyInjection;
1112
using Microsoft.Extensions.FileProviders;
13+
using static Microsoft.AspNetCore.Internal.LinkerFlags;
1214

1315
namespace Microsoft.AspNetCore.Builder;
1416

1517
/// <summary>
16-
///
18+
///
1719
/// </summary>
1820
public static class RazorComponentsEndpointRouteBuilderExtensions
1921
{
2022
/// <summary>
21-
///
23+
///
2224
/// </summary>
2325
/// <param name="endpoints"></param>
2426
/// <returns></returns>
25-
public static RazorComponentEndpointConventionBuilder MapRazorComponents<TRootComponent>(this IEndpointRouteBuilder endpoints)
27+
public static RazorComponentEndpointConventionBuilder MapRazorComponents<[DynamicallyAccessedMembers(Component)] TRootComponent>(this IEndpointRouteBuilder endpoints)
2628
{
2729
ArgumentNullException.ThrowIfNull(endpoints);
2830

@@ -63,7 +65,7 @@ private static void AddBlazorWebJsEndpoint(IEndpointRouteBuilder endpoints)
6365
#endif
6466
}
6567

66-
private static RazorComponentEndpointDataSource<TRootComponent> GetOrCreateDataSource<TRootComponent>(IEndpointRouteBuilder endpoints)
68+
private static RazorComponentEndpointDataSource<TRootComponent> GetOrCreateDataSource<[DynamicallyAccessedMembers(Component)] TRootComponent>(IEndpointRouteBuilder endpoints)
6769
{
6870
var dataSource = endpoints.DataSources.OfType<RazorComponentEndpointDataSource<TRootComponent>>().FirstOrDefault();
6971
if (dataSource == null)

src/Components/Endpoints/src/Builder/RenderModeEndpointProvider.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
// Licensed to the .NET Foundation under one or more agreements.
22
// The .NET Foundation licenses this file to you under the MIT license.
33

4+
using System.Diagnostics.CodeAnalysis;
45
using Microsoft.AspNetCore.Builder;
56
using Microsoft.AspNetCore.Http;
67
using Microsoft.AspNetCore.Routing;
8+
using static Microsoft.AspNetCore.Internal.LinkerFlags;
79

810
namespace Microsoft.AspNetCore.Components.Endpoints;
911

@@ -31,7 +33,7 @@ public abstract IEnumerable<RouteEndpointBuilder> GetEndpointBuilders(
3133

3234
internal static void AddEndpoints(
3335
List<Endpoint> endpoints,
34-
Type rootComponent,
36+
[DynamicallyAccessedMembers(Component)] Type rootComponent,
3537
IEnumerable<RouteEndpointBuilder> renderModeEndpoints,
3638
IComponentRenderMode renderMode,
3739
List<Action<EndpointBuilder>> conventions,

src/Components/Endpoints/src/Builder/RootComponentMetadata.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
// Licensed to the .NET Foundation under one or more agreements.
22
// The .NET Foundation licenses this file to you under the MIT license.
33

4+
using System.Diagnostics.CodeAnalysis;
5+
using static Microsoft.AspNetCore.Internal.LinkerFlags;
6+
47
namespace Microsoft.AspNetCore.Components.Endpoints;
58

69
/// <summary>
@@ -12,13 +15,14 @@ public class RootComponentMetadata
1215
/// Initializes a new instance of <see cref="RootComponentMetadata"/>.
1316
/// </summary>
1417
/// <param name="rootComponentType">The component type.</param>
15-
public RootComponentMetadata(Type rootComponentType)
18+
public RootComponentMetadata([DynamicallyAccessedMembers(Component)] Type rootComponentType)
1619
{
1720
Type = rootComponentType;
1821
}
1922

2023
/// <summary>
2124
/// Gets the component type.
2225
/// </summary>
26+
[DynamicallyAccessedMembers(Component)]
2327
public Type Type { get; }
2428
}

src/Components/Endpoints/src/DependencyInjection/IComponentPrerenderer.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
// Licensed to the .NET Foundation under one or more agreements.
22
// The .NET Foundation licenses this file to you under the MIT license.
33

4+
using System.Diagnostics.CodeAnalysis;
45
using Microsoft.AspNetCore.Html;
56
using Microsoft.AspNetCore.Http;
7+
using static Microsoft.AspNetCore.Internal.LinkerFlags;
68

79
namespace Microsoft.AspNetCore.Components.Endpoints;
810

@@ -21,7 +23,7 @@ public interface IComponentPrerenderer
2123
/// <returns>A task that completes with the prerendered content.</returns>
2224
ValueTask<IHtmlAsyncContent> PrerenderComponentAsync(
2325
HttpContext httpContext,
24-
Type componentType,
26+
[DynamicallyAccessedMembers(Component)] Type componentType,
2527
IComponentRenderMode renderMode,
2628
ParameterView parameters);
2729

src/Components/Endpoints/src/Discovery/PageComponentBuilder.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22
// The .NET Foundation licenses this file to you under the MIT license.
33

44
using System.Diagnostics;
5+
using System.Diagnostics.CodeAnalysis;
56
using System.Linq;
7+
using static Microsoft.AspNetCore.Internal.LinkerFlags;
68

79
namespace Microsoft.AspNetCore.Components.Discovery;
810

@@ -35,6 +37,7 @@ public required IReadOnlyList<string> RouteTemplates
3537
/// <summary>
3638
/// Gets or sets the page type.
3739
/// </summary>
40+
[DynamicallyAccessedMembers(Component)]
3841
public required Type PageType { get; set; }
3942

4043
/// <summary>

src/Components/Endpoints/src/Discovery/PageComponentInfo.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
// The .NET Foundation licenses this file to you under the MIT license.
33

44
using System.Diagnostics;
5+
using System.Diagnostics.CodeAnalysis;
6+
using static Microsoft.AspNetCore.Internal.LinkerFlags;
57

68
namespace Microsoft.AspNetCore.Components.Discovery;
79

@@ -20,7 +22,7 @@ internal class PageComponentInfo
2022
/// <param name="metadata">The page metadata.</param>
2123
internal PageComponentInfo(
2224
string displayName,
23-
Type type,
25+
[DynamicallyAccessedMembers(Component)] Type type,
2426
string route,
2527
IReadOnlyList<object> metadata)
2628
{
@@ -38,6 +40,7 @@ internal PageComponentInfo(
3840
/// <summary>
3941
/// Gets the page type.
4042
/// </summary>
43+
[DynamicallyAccessedMembers(Component)]
4144
public Type Type { get; }
4245

4346
/// <summary>

src/Components/Endpoints/src/Microsoft.AspNetCore.Components.Endpoints.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
<Compile Include="$(RepoRoot)src\Shared\ClosedGenericMatcher\ClosedGenericMatcher.cs" LinkBase="FormMapping" />
3636
<Compile Include="$(ComponentsSharedSourceRoot)src\CacheHeaderSettings.cs" Link="Shared\CacheHeaderSettings.cs" />
3737
<Compile Include="$(ComponentsSharedSourceRoot)src\DefaultAntiforgeryStateProvider.cs" LinkBase="Forms" />
38+
<Compile Include="$(SharedSourceRoot)LinkerFlags.cs" LinkBase="Shared" />
3839

3940
<Compile Include="$(SharedSourceRoot)PropertyHelper\**\*.cs" />
4041

src/Components/Endpoints/src/RazorComponentEndpointHost.cs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
// Licensed to the .NET Foundation under one or more agreements.
22
// The .NET Foundation licenses this file to you under the MIT license.
33

4+
using System.Diagnostics.CodeAnalysis;
45
using System.Reflection;
56
using Microsoft.AspNetCore.Components.Rendering;
7+
using static Microsoft.AspNetCore.Internal.LinkerFlags;
68

79
namespace Microsoft.AspNetCore.Components.Endpoints;
810

@@ -19,8 +21,12 @@ internal class RazorComponentEndpointHost : IComponent
1921
{
2022
private RenderHandle _renderHandle;
2123

22-
[Parameter] public Type ComponentType { get; set; } = default!;
23-
[Parameter] public IReadOnlyDictionary<string, object?>? ComponentParameters { get; set; }
24+
[Parameter]
25+
[DynamicallyAccessedMembers(Component)]
26+
public Type ComponentType { get; set; } = default!;
27+
28+
[Parameter]
29+
public IReadOnlyDictionary<string, object?>? ComponentParameters { get; set; }
2430

2531
public void Attach(RenderHandle renderHandle)
2632
=> _renderHandle = renderHandle;

src/Components/Endpoints/src/Rendering/EndpointHtmlRenderer.Prerendering.cs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,20 @@
11
// Licensed to the .NET Foundation under one or more agreements.
22
// The .NET Foundation licenses this file to you under the MIT license.
33

4+
using System.Diagnostics.CodeAnalysis;
45
using System.Text.Encodings.Web;
56
using Microsoft.AspNetCore.Components.Web.HtmlRendering;
67
using Microsoft.AspNetCore.Html;
78
using Microsoft.AspNetCore.Http;
9+
using static Microsoft.AspNetCore.Internal.LinkerFlags;
810

911
namespace Microsoft.AspNetCore.Components.Endpoints;
1012

1113
internal partial class EndpointHtmlRenderer
1214
{
1315
private static readonly object ComponentSequenceKey = new object();
1416

15-
protected override IComponent ResolveComponentForRenderMode(Type componentType, int? parentComponentId, IComponentActivator componentActivator, IComponentRenderMode renderMode)
17+
protected override IComponent ResolveComponentForRenderMode([DynamicallyAccessedMembers(Component)] Type componentType, int? parentComponentId, IComponentActivator componentActivator, IComponentRenderMode renderMode)
1618
{
1719
var closestRenderModeBoundary = parentComponentId.HasValue
1820
? GetClosestRenderModeBoundary(parentComponentId.Value)
@@ -50,14 +52,14 @@ protected override IComponent ResolveComponentForRenderMode(Type componentType,
5052

5153
public ValueTask<IHtmlAsyncContent> PrerenderComponentAsync(
5254
HttpContext httpContext,
53-
Type componentType,
55+
[DynamicallyAccessedMembers(Component)] Type componentType,
5456
IComponentRenderMode prerenderMode,
5557
ParameterView parameters)
5658
=> PrerenderComponentAsync(httpContext, componentType, prerenderMode, parameters, waitForQuiescence: true);
5759

5860
public async ValueTask<IHtmlAsyncContent> PrerenderComponentAsync(
5961
HttpContext httpContext,
60-
Type componentType,
62+
[DynamicallyAccessedMembers(Component)] Type componentType,
6163
IComponentRenderMode? prerenderMode,
6264
ParameterView parameters,
6365
bool waitForQuiescence)
@@ -98,7 +100,7 @@ public async ValueTask<IHtmlAsyncContent> PrerenderComponentAsync(
98100

99101
internal async ValueTask<PrerenderedComponentHtmlContent> RenderEndpointComponent(
100102
HttpContext httpContext,
101-
Type rootComponentType,
103+
[DynamicallyAccessedMembers(Component)] Type rootComponentType,
102104
ParameterView parameters,
103105
bool waitForQuiescence)
104106
{

src/Components/Endpoints/src/Rendering/EndpointHtmlRenderer.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
using Microsoft.Extensions.DependencyInjection;
1919
using Microsoft.Extensions.Logging;
2020
using Microsoft.Extensions.Primitives;
21+
using static Microsoft.AspNetCore.Internal.LinkerFlags;
2122

2223
namespace Microsoft.AspNetCore.Components.Endpoints;
2324

@@ -65,7 +66,7 @@ private void SetHttpContext(HttpContext httpContext)
6566

6667
internal static async Task InitializeStandardComponentServicesAsync(
6768
HttpContext httpContext,
68-
Type? componentType = null,
69+
[DynamicallyAccessedMembers(Component)] Type? componentType = null,
6970
string? handler = null,
7071
IFormCollection? form = null)
7172
{

src/Components/Endpoints/src/Rendering/SSRRenderModeBoundary.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,15 @@
33

44
using System.Collections.Concurrent;
55
using System.Diagnostics;
6+
using System.Diagnostics.CodeAnalysis;
67
using System.Globalization;
78
using System.Security.Cryptography;
89
using System.Text;
910
using Microsoft.AspNetCore.Components.Rendering;
1011
using Microsoft.AspNetCore.Components.Web;
1112
using Microsoft.AspNetCore.Http;
1213
using Microsoft.Extensions.DependencyInjection;
14+
using static Microsoft.AspNetCore.Internal.LinkerFlags;
1315

1416
namespace Microsoft.AspNetCore.Components.Endpoints;
1517

@@ -21,14 +23,15 @@ internal class SSRRenderModeBoundary : IComponent
2123
{
2224
private static readonly ConcurrentDictionary<Type, string> _componentTypeNameHashCache = new();
2325

26+
[DynamicallyAccessedMembers(Component)]
2427
private readonly Type _componentType;
2528
private readonly IComponentRenderMode _renderMode;
2629
private readonly bool _prerender;
2730
private RenderHandle _renderHandle;
2831
private IReadOnlyDictionary<string, object?>? _latestParameters;
2932
private string? _markerKey;
3033

31-
public SSRRenderModeBoundary(Type componentType, IComponentRenderMode renderMode)
34+
public SSRRenderModeBoundary([DynamicallyAccessedMembers(Component)] Type componentType, IComponentRenderMode renderMode)
3235
{
3336
_componentType = componentType;
3437
_renderMode = renderMode;

0 commit comments

Comments
 (0)