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