|
| 1 | +// Licensed to the .NET Foundation under one or more agreements. |
| 2 | +// The .NET Foundation licenses this file to you under the MIT license. |
| 3 | + |
| 4 | +#nullable enable |
| 5 | + |
| 6 | +using System; |
| 7 | +using System.Diagnostics.CodeAnalysis; |
| 8 | +using System.Runtime.CompilerServices; |
| 9 | +using System.Reflection; |
| 10 | + |
| 11 | +/// <summary> |
| 12 | +/// Ensures setting _AggressiveAttributeTrimming = true causes various attributes to be trimmed |
| 13 | +/// </summary> |
| 14 | +class Program |
| 15 | +{ |
| 16 | + [UnconditionalSuppressMessage ("ReflectionAnalysis", "IL2111", Justification = "Expected trim warning for reflection over annotated members.")] |
| 17 | + [UnconditionalSuppressMessage ("ReflectionAnalysis", "IL2026", Justification = "Expected trim warning for reflection over annotated members.")] |
| 18 | + static int Main(string[] args) |
| 19 | + { |
| 20 | + // Reference to IsDynamicCodeSupported (which has FeatureGuard(typeof(RequiresDynamicCodeAttribute))) |
| 21 | + // should not produce a warning because both RequiresDynamicCodeAttribute and FeatureGuardAttribute are removed. |
| 22 | + if (RuntimeFeature.IsDynamicCodeSupported) |
| 23 | + { |
| 24 | + UseDynamicCode(); |
| 25 | + } |
| 26 | + |
| 27 | + // Check that a few attribute instances are indeed removed |
| 28 | + CheckRemovedAttributes(typeof(MembersWithRemovedAttributes)); |
| 29 | + |
| 30 | + return 100; |
| 31 | + } |
| 32 | + |
| 33 | + [RequiresDynamicCode(nameof(UseDynamicCode))] |
| 34 | + static void UseDynamicCode() { } |
| 35 | + |
| 36 | + class MembersWithRemovedAttributes |
| 37 | + { |
| 38 | + static void DynamicallyAccessedMembers([DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicMethods)] Type t) { } |
| 39 | + |
| 40 | + [FeatureGuard(typeof(RequiresUnreferencedCodeAttribute))] |
| 41 | + static bool FeatureGuard => throw null!; |
| 42 | + |
| 43 | + [FeatureSwitchDefinition("Program.MembersWithRemovedAttributes.FeatureSwitchDefinition")] |
| 44 | + static bool FeatureSwitchDefinition => throw null!; |
| 45 | + |
| 46 | + [RequiresDynamicCode(nameof(RequiresDynamicCode))] |
| 47 | + static void RequiresDynamicCode() { } |
| 48 | + |
| 49 | + [RequiresUnreferencedCode(nameof(RequiresUnreferencedCode))] |
| 50 | + static void RequiresUnreferencedCode() { } |
| 51 | + } |
| 52 | + |
| 53 | + static void CheckRemovedAttributes([DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)] Type type) |
| 54 | + { |
| 55 | + Console.WriteLine($"Validating {type}"); |
| 56 | + foreach (var member in type.GetMembers(BindingFlags.Static | BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.DeclaredOnly)) |
| 57 | + { |
| 58 | + CheckRemovedAttributes(member); |
| 59 | + |
| 60 | + if (member is MethodInfo method) |
| 61 | + { |
| 62 | + foreach (var parameter in method.GetParameters()) |
| 63 | + { |
| 64 | + CheckRemovedAttributes(parameter); |
| 65 | + } |
| 66 | + } |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + static void CheckRemovedAttributes(ICustomAttributeProvider provider) |
| 71 | + { |
| 72 | + foreach (var attribute in provider.GetCustomAttributes(false)) |
| 73 | + { |
| 74 | + if (attribute is NullableContextAttribute) |
| 75 | + continue; |
| 76 | + |
| 77 | + throw new Exception($"Unexpected attribute {attribute.GetType()} on {provider}"); |
| 78 | + } |
| 79 | + } |
| 80 | +} |
0 commit comments