Skip to content

Commit 231255d

Browse files
authored
EnableAOTAnalyzer for most of Microsoft.Extensions libraries (#73737)
The only libraries that aren't enabled yet are DependencyInjection and Hosting. These will come in a separate PR. Contributes to #71654
1 parent 0d4f624 commit 231255d

File tree

48 files changed

+180
-38
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

48 files changed

+180
-38
lines changed

src/libraries/Microsoft.Extensions.Caching.Abstractions/src/Microsoft.Extensions.Caching.Abstractions.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
<TargetFrameworks>$(NetCoreAppCurrent);$(NetCoreAppMinimum);netstandard2.0;$(NetFrameworkMinimum)</TargetFrameworks>
55
<EnableDefaultItems>true</EnableDefaultItems>
66
<IsPackable>true</IsPackable>
7+
<EnableAOTAnalyzer>true</EnableAOTAnalyzer>
78
<PackageDescription>Caching abstractions for in-memory cache and distributed cache.
89

910
Commonly Used Types:

src/libraries/Microsoft.Extensions.Caching.Memory/src/Microsoft.Extensions.Caching.Memory.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
<TargetFrameworks>$(NetCoreAppCurrent);$(NetCoreAppMinimum);netstandard2.0;$(NetFrameworkMinimum)</TargetFrameworks>
55
<EnableDefaultItems>true</EnableDefaultItems>
66
<IsPackable>true</IsPackable>
7+
<EnableAOTAnalyzer>true</EnableAOTAnalyzer>
78
<PackageDescription>In-memory cache implementation of Microsoft.Extensions.Caching.Memory.IMemoryCache.</PackageDescription>
89
</PropertyGroup>
910

src/libraries/Microsoft.Extensions.Configuration.Abstractions/src/Microsoft.Extensions.Configuration.Abstractions.csproj

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1-
<Project Sdk="Microsoft.NET.Sdk">
1+
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
44
<TargetFrameworks>$(NetCoreAppCurrent);$(NetCoreAppMinimum);netstandard2.0;$(NetFrameworkMinimum)</TargetFrameworks>
5-
<IsPackable>true</IsPackable>
65
<EnableDefaultItems>true</EnableDefaultItems>
6+
<IsPackable>true</IsPackable>
7+
<EnableAOTAnalyzer>true</EnableAOTAnalyzer>
78
<PackageDescription>Abstractions of key-value pair based configuration.
89

910
Commonly Used Types:

src/libraries/Microsoft.Extensions.Configuration.Binder/ref/Microsoft.Extensions.Configuration.Binder.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,19 @@ public BinderOptions() { }
1414
}
1515
public static partial class ConfigurationBinder
1616
{
17+
[System.Diagnostics.CodeAnalysis.RequiresDynamicCodeAttribute("Binding strongly typed objects to configuration values requires generating dynamic code at runtime, for example instantiating generic types.")]
1718
[System.Diagnostics.CodeAnalysis.RequiresUnreferencedCodeAttribute("Cannot statically analyze the type of instance so its members may be trimmed")]
1819
public static void Bind(this Microsoft.Extensions.Configuration.IConfiguration configuration, object? instance) { }
20+
[System.Diagnostics.CodeAnalysis.RequiresDynamicCodeAttribute("Binding strongly typed objects to configuration values requires generating dynamic code at runtime, for example instantiating generic types.")]
1921
[System.Diagnostics.CodeAnalysis.RequiresUnreferencedCodeAttribute("Cannot statically analyze the type of instance so its members may be trimmed")]
2022
public static void Bind(this Microsoft.Extensions.Configuration.IConfiguration configuration, object? instance, System.Action<Microsoft.Extensions.Configuration.BinderOptions>? configureOptions) { }
23+
[System.Diagnostics.CodeAnalysis.RequiresDynamicCodeAttribute("Binding strongly typed objects to configuration values requires generating dynamic code at runtime, for example instantiating generic types.")]
2124
[System.Diagnostics.CodeAnalysis.RequiresUnreferencedCodeAttribute("Cannot statically analyze the type of instance so its members may be trimmed")]
2225
public static void Bind(this Microsoft.Extensions.Configuration.IConfiguration configuration, string key, object? instance) { }
26+
[System.Diagnostics.CodeAnalysis.RequiresDynamicCodeAttribute("Binding strongly typed objects to configuration values requires generating dynamic code at runtime, for example instantiating generic types.")]
2327
[System.Diagnostics.CodeAnalysis.RequiresUnreferencedCodeAttribute("In case the type is non-primitive, the trimmer cannot statically analyze the object's type so its members may be trimmed.")]
2428
public static object? Get(this Microsoft.Extensions.Configuration.IConfiguration configuration, System.Type type) { throw null; }
29+
[System.Diagnostics.CodeAnalysis.RequiresDynamicCodeAttribute("Binding strongly typed objects to configuration values requires generating dynamic code at runtime, for example instantiating generic types.")]
2530
[System.Diagnostics.CodeAnalysis.RequiresUnreferencedCodeAttribute("In case the type is non-primitive, the trimmer cannot statically analyze the object's type so its members may be trimmed.")]
2631
public static object? Get(this Microsoft.Extensions.Configuration.IConfiguration configuration, [System.Diagnostics.CodeAnalysis.DynamicallyAccessedMembersAttribute(System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.All)] System.Type type, System.Action<Microsoft.Extensions.Configuration.BinderOptions>? configureOptions) { throw null; }
2732
[System.Diagnostics.CodeAnalysis.RequiresUnreferencedCodeAttribute("In case the type is non-primitive, the trimmer cannot statically analyze the object's type so its members may be trimmed.")]
@@ -32,8 +37,10 @@ public static void Bind(this Microsoft.Extensions.Configuration.IConfiguration c
3237
public static T? GetValue<[System.Diagnostics.CodeAnalysis.DynamicallyAccessedMembersAttribute(System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.All)] T>(this Microsoft.Extensions.Configuration.IConfiguration configuration, string key) { throw null; }
3338
[System.Diagnostics.CodeAnalysis.RequiresUnreferencedCodeAttribute("In case the type is non-primitive, the trimmer cannot statically analyze the object's type so its members may be trimmed.")]
3439
public static T? GetValue<[System.Diagnostics.CodeAnalysis.DynamicallyAccessedMembersAttribute(System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.All)] T>(this Microsoft.Extensions.Configuration.IConfiguration configuration, string key, T defaultValue) { throw null; }
40+
[System.Diagnostics.CodeAnalysis.RequiresDynamicCodeAttribute("Binding strongly typed objects to configuration values requires generating dynamic code at runtime, for example instantiating generic types.")]
3541
[System.Diagnostics.CodeAnalysis.RequiresUnreferencedCodeAttribute("In case the type is non-primitive, the trimmer cannot statically analyze the object's type so its members may be trimmed.")]
3642
public static T? Get<[System.Diagnostics.CodeAnalysis.DynamicallyAccessedMembersAttribute(System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.All)] T>(this Microsoft.Extensions.Configuration.IConfiguration configuration) { throw null; }
43+
[System.Diagnostics.CodeAnalysis.RequiresDynamicCodeAttribute("Binding strongly typed objects to configuration values requires generating dynamic code at runtime, for example instantiating generic types.")]
3744
[System.Diagnostics.CodeAnalysis.RequiresUnreferencedCodeAttribute("In case the type is non-primitive, the trimmer cannot statically analyze the object's type so its members may be trimmed.")]
3845
public static T? Get<[System.Diagnostics.CodeAnalysis.DynamicallyAccessedMembersAttribute(System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.All)] T>(this Microsoft.Extensions.Configuration.IConfiguration configuration, System.Action<Microsoft.Extensions.Configuration.BinderOptions>? configureOptions) { throw null; }
3946
}

src/libraries/Microsoft.Extensions.Configuration.Binder/ref/Microsoft.Extensions.Configuration.Binder.csproj

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,11 @@
1212
<Compile Include="$(CoreLibSharedDir)System\Diagnostics\CodeAnalysis\DynamicallyAccessedMemberTypes.cs" />
1313
<Compile Include="$(CoreLibSharedDir)System\Diagnostics\CodeAnalysis\RequiresUnreferencedCodeAttribute.cs" />
1414
</ItemGroup>
15-
15+
16+
<ItemGroup Condition="!$([MSBuild]::IsTargetFrameworkCompatible('$(TargetFramework)', 'net7.0'))">
17+
<Compile Include="$(CoreLibSharedDir)System\Diagnostics\CodeAnalysis\RequiresDynamicCodeAttribute.cs" />
18+
</ItemGroup>
19+
1620
<ItemGroup>
1721
<ProjectReference Include="$(LibrariesProjectRoot)Microsoft.Extensions.Configuration.Abstractions\ref\Microsoft.Extensions.Configuration.Abstractions.csproj" />
1822
</ItemGroup>

src/libraries/Microsoft.Extensions.Configuration.Binder/src/ConfigurationBinder.cs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ namespace Microsoft.Extensions.Configuration
1919
public static class ConfigurationBinder
2020
{
2121
private const BindingFlags DeclaredOnlyLookup = BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Static | BindingFlags.DeclaredOnly;
22+
private const string DynamicCodeWarningMessage = "Binding strongly typed objects to configuration values requires generating dynamic code at runtime, for example instantiating generic types.";
2223
private const string TrimmingWarningMessage = "In case the type is non-primitive, the trimmer cannot statically analyze the object's type so its members may be trimmed.";
2324
private const string InstanceGetTypeTrimmingWarningMessage = "Cannot statically analyze the type of instance so its members may be trimmed";
2425
private const string PropertyTrimmingWarningMessage = "Cannot statically analyze property.PropertyType so its members may be trimmed.";
@@ -31,6 +32,7 @@ public static class ConfigurationBinder
3132
/// <typeparam name="T">The type of the new instance to bind.</typeparam>
3233
/// <param name="configuration">The configuration instance to bind.</param>
3334
/// <returns>The new instance of T if successful, default(T) otherwise.</returns>
35+
[RequiresDynamicCode(DynamicCodeWarningMessage)]
3436
[RequiresUnreferencedCode(TrimmingWarningMessage)]
3537
public static T? Get<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)] T>(this IConfiguration configuration)
3638
=> configuration.Get<T>(_ => { });
@@ -44,6 +46,7 @@ public static class ConfigurationBinder
4446
/// <param name="configuration">The configuration instance to bind.</param>
4547
/// <param name="configureOptions">Configures the binder options.</param>
4648
/// <returns>The new instance of T if successful, default(T) otherwise.</returns>
49+
[RequiresDynamicCode(DynamicCodeWarningMessage)]
4750
[RequiresUnreferencedCode(TrimmingWarningMessage)]
4851
public static T? Get<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)] T>(this IConfiguration configuration, Action<BinderOptions>? configureOptions)
4952
{
@@ -65,6 +68,7 @@ public static class ConfigurationBinder
6568
/// <param name="configuration">The configuration instance to bind.</param>
6669
/// <param name="type">The type of the new instance to bind.</param>
6770
/// <returns>The new instance if successful, null otherwise.</returns>
71+
[RequiresDynamicCode(DynamicCodeWarningMessage)]
6872
[RequiresUnreferencedCode(TrimmingWarningMessage)]
6973
public static object? Get(this IConfiguration configuration, Type type)
7074
=> configuration.Get(type, _ => { });
@@ -78,6 +82,7 @@ public static class ConfigurationBinder
7882
/// <param name="type">The type of the new instance to bind.</param>
7983
/// <param name="configureOptions">Configures the binder options.</param>
8084
/// <returns>The new instance if successful, null otherwise.</returns>
85+
[RequiresDynamicCode(DynamicCodeWarningMessage)]
8186
[RequiresUnreferencedCode(TrimmingWarningMessage)]
8287
public static object? Get(
8388
this IConfiguration configuration,
@@ -100,6 +105,7 @@ public static class ConfigurationBinder
100105
/// <param name="configuration">The configuration instance to bind.</param>
101106
/// <param name="key">The key of the configuration section to bind.</param>
102107
/// <param name="instance">The object to bind.</param>
108+
[RequiresDynamicCode(DynamicCodeWarningMessage)]
103109
[RequiresUnreferencedCode(InstanceGetTypeTrimmingWarningMessage)]
104110
public static void Bind(this IConfiguration configuration, string key, object? instance)
105111
=> configuration.GetSection(key).Bind(instance);
@@ -109,6 +115,7 @@ public static void Bind(this IConfiguration configuration, string key, object? i
109115
/// </summary>
110116
/// <param name="configuration">The configuration instance to bind.</param>
111117
/// <param name="instance">The object to bind.</param>
118+
[RequiresDynamicCode(DynamicCodeWarningMessage)]
112119
[RequiresUnreferencedCode(InstanceGetTypeTrimmingWarningMessage)]
113120
public static void Bind(this IConfiguration configuration, object? instance)
114121
=> configuration.Bind(instance, _ => { });
@@ -119,6 +126,7 @@ public static void Bind(this IConfiguration configuration, object? instance)
119126
/// <param name="configuration">The configuration instance to bind.</param>
120127
/// <param name="instance">The object to bind.</param>
121128
/// <param name="configureOptions">Configures the binder options.</param>
129+
[RequiresDynamicCode(DynamicCodeWarningMessage)]
122130
[RequiresUnreferencedCode(InstanceGetTypeTrimmingWarningMessage)]
123131
public static void Bind(this IConfiguration configuration, object? instance, Action<BinderOptions>? configureOptions)
124132
{
@@ -201,6 +209,7 @@ public static void Bind(this IConfiguration configuration, object? instance, Act
201209
return defaultValue;
202210
}
203211

212+
[RequiresDynamicCode(DynamicCodeWarningMessage)]
204213
[RequiresUnreferencedCode(PropertyTrimmingWarningMessage)]
205214
private static void BindProperties(object instance, IConfiguration configuration, BinderOptions options)
206215
{
@@ -231,6 +240,7 @@ private static void BindProperties(object instance, IConfiguration configuration
231240
}
232241
}
233242

243+
[RequiresDynamicCode(DynamicCodeWarningMessage)]
234244
[RequiresUnreferencedCode(PropertyTrimmingWarningMessage)]
235245
private static void BindProperty(PropertyInfo property, object instance, IConfiguration config, BinderOptions options)
236246
{
@@ -258,6 +268,7 @@ private static void BindProperty(PropertyInfo property, object instance, IConfig
258268
}
259269
}
260270

271+
[RequiresDynamicCode(DynamicCodeWarningMessage)]
261272
[RequiresUnreferencedCode(TrimmingWarningMessage)]
262273
private static void BindInstance(
263274
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)] Type type,
@@ -368,6 +379,7 @@ private static void BindInstance(
368379
}
369380
}
370381

382+
[RequiresDynamicCode(DynamicCodeWarningMessage)]
371383
[RequiresUnreferencedCode(
372384
"In case type is a Nullable<T>, cannot statically analyze what the underlying type is so its members may be trimmed.")]
373385
private static object CreateInstance(
@@ -480,6 +492,7 @@ private static bool CanBindToTheseConstructorParameters(ParameterInfo[] construc
480492
return true;
481493
}
482494

495+
[RequiresDynamicCode(DynamicCodeWarningMessage)]
483496
[RequiresUnreferencedCode("Cannot statically analyze what the element type is of the value objects in the dictionary so its members may be trimmed.")]
484497
private static object? BindDictionaryInterface(
485498
object? source,
@@ -542,6 +555,7 @@ private static bool CanBindToTheseConstructorParameters(ParameterInfo[] construc
542555
// When a user specifies a concrete dictionary in their config class, then that
543556
// value is used as-us. When a user specifies an interface (instantiated) in their config class,
544557
// then it is cloned to a new dictionary, the same way as other collections.
558+
[RequiresDynamicCode(DynamicCodeWarningMessage)]
545559
[RequiresUnreferencedCode("Cannot statically analyze what the element type is of the value objects in the dictionary so its members may be trimmed.")]
546560
private static void BindConcreteDictionary(
547561
object? dictionary,
@@ -603,6 +617,7 @@ private static void BindConcreteDictionary(
603617
}
604618
}
605619

620+
[RequiresDynamicCode(DynamicCodeWarningMessage)]
606621
[RequiresUnreferencedCode("Cannot statically analyze what the element type is of the object collection so its members may be trimmed.")]
607622
private static void BindCollection(
608623
object collection,
@@ -635,6 +650,7 @@ private static void BindCollection(
635650
}
636651
}
637652

653+
[RequiresDynamicCode(DynamicCodeWarningMessage)]
638654
[RequiresUnreferencedCode("Cannot statically analyze what the element type is of the Array so its members may be trimmed.")]
639655
private static Array BindArray(Type type, IEnumerable? source, IConfiguration config, BinderOptions options)
640656
{
@@ -687,6 +703,7 @@ private static Array BindArray(Type type, IEnumerable? source, IConfiguration co
687703
return result;
688704
}
689705

706+
[RequiresDynamicCode(DynamicCodeWarningMessage)]
690707
[RequiresUnreferencedCode("Cannot statically analyze what the element type is of the Array so its members may be trimmed.")]
691708
private static object? BindSet(Type type, IEnumerable? source, IConfiguration config, BinderOptions options)
692709
{
@@ -893,6 +910,7 @@ private static List<PropertyInfo> GetAllProperties([DynamicallyAccessedMembers(D
893910
return allProperties;
894911
}
895912

913+
[RequiresDynamicCode(DynamicCodeWarningMessage)]
896914
[RequiresUnreferencedCode(PropertyTrimmingWarningMessage)]
897915
private static object? BindParameter(ParameterInfo parameter, Type type, IConfiguration config,
898916
BinderOptions options)

src/libraries/Microsoft.Extensions.Configuration.Binder/src/Microsoft.Extensions.Configuration.Binder.csproj

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
<TargetFrameworks>$(NetCoreAppCurrent);$(NetCoreAppMinimum);netstandard2.0;$(NetFrameworkMinimum)</TargetFrameworks>
55
<EnableDefaultItems>true</EnableDefaultItems>
66
<IsPackable>true</IsPackable>
7+
<EnableAOTAnalyzer>true</EnableAOTAnalyzer>
78
<PackageDescription>Functionality to bind an object to data in configuration providers for Microsoft.Extensions.Configuration.</PackageDescription>
89
</PropertyGroup>
910

@@ -21,6 +22,10 @@
2122
<Compile Include="$(CoreLibSharedDir)System\Diagnostics\CodeAnalysis\UnconditionalSuppressMessageAttribute.cs" />
2223
</ItemGroup>
2324

25+
<ItemGroup Condition="!$([MSBuild]::IsTargetFrameworkCompatible('$(TargetFramework)', 'net7.0'))">
26+
<Compile Include="$(CoreLibSharedDir)System\Diagnostics\CodeAnalysis\RequiresDynamicCodeAttribute.cs" />
27+
</ItemGroup>
28+
2429
<ItemGroup Condition="'$(TargetFrameworkIdentifier)' == '.NETCoreApp'">
2530
<Compile Include="$(CommonPath)Extensions\ParameterDefaultValue\ParameterDefaultValue.netcoreapp.cs" Link="Common\src\Extensions\ParameterDefaultValue\ParameterDefaultValue.netcoreapp.cs" />
2631
</ItemGroup>

src/libraries/Microsoft.Extensions.Configuration.CommandLine/src/Microsoft.Extensions.Configuration.CommandLine.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
<TargetFrameworks>$(NetCoreAppCurrent);$(NetCoreAppMinimum);netstandard2.0;$(NetFrameworkMinimum)</TargetFrameworks>
55
<EnableDefaultItems>true</EnableDefaultItems>
66
<IsPackable>true</IsPackable>
7+
<EnableAOTAnalyzer>true</EnableAOTAnalyzer>
78
<PackageDescription>Command line configuration provider implementation for Microsoft.Extensions.Configuration.</PackageDescription>
89
</PropertyGroup>
910

src/libraries/Microsoft.Extensions.Configuration.EnvironmentVariables/src/Microsoft.Extensions.Configuration.EnvironmentVariables.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
<TargetFrameworks>$(NetCoreAppCurrent);$(NetCoreAppMinimum);netstandard2.0;$(NetFrameworkMinimum)</TargetFrameworks>
55
<EnableDefaultItems>true</EnableDefaultItems>
66
<IsPackable>true</IsPackable>
7+
<EnableAOTAnalyzer>true</EnableAOTAnalyzer>
78
<PackageDescription>Environment variables configuration provider implementation for Microsoft.Extensions.Configuration.</PackageDescription>
89
</PropertyGroup>
910

src/libraries/Microsoft.Extensions.Configuration.FileExtensions/src/Microsoft.Extensions.Configuration.FileExtensions.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
<TargetFrameworks>$(NetCoreAppCurrent);$(NetCoreAppMinimum);netstandard2.0;$(NetFrameworkMinimum)</TargetFrameworks>
55
<EnableDefaultItems>true</EnableDefaultItems>
66
<IsPackable>true</IsPackable>
7+
<EnableAOTAnalyzer>true</EnableAOTAnalyzer>
78
<PackageDescription>Extension methods for configuring file-based configuration providers for Microsoft.Extensions.Configuration.</PackageDescription>
89
</PropertyGroup>
910

0 commit comments

Comments
 (0)