Skip to content

Commit dd14ca6

Browse files
authored
Annotate the library with trimming attributes (App-vNext#1414)
1 parent 38d903e commit dd14ca6

30 files changed

+580
-37
lines changed
Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
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+
#pragma warning disable IDE0079
5+
#pragma warning disable SA1101
6+
#pragma warning disable SA1116
7+
#pragma warning disable SA1117
8+
#pragma warning disable SA1512
9+
#pragma warning disable SA1623
10+
#pragma warning disable SA1642
11+
#pragma warning disable S3903
12+
13+
namespace System.Diagnostics.CodeAnalysis;
14+
15+
/// <summary>
16+
/// States a dependency that one member has on another.
17+
/// </summary>
18+
/// <remarks>
19+
/// This can be used to inform tooling of a dependency that is otherwise not evident purely from
20+
/// metadata and IL, for example a member relied on via reflection.
21+
/// </remarks>
22+
[AttributeUsage(
23+
AttributeTargets.Constructor | AttributeTargets.Field | AttributeTargets.Method,
24+
AllowMultiple = true, Inherited = false)]
25+
[ExcludeFromCodeCoverage]
26+
internal sealed class DynamicDependencyAttribute : Attribute
27+
{
28+
/// <summary>
29+
/// Initializes a new instance of the <see cref="DynamicDependencyAttribute"/> class
30+
/// with the specified signature of a member on the same type as the consumer.
31+
/// </summary>
32+
/// <param name="memberSignature">The signature of the member depended on.</param>
33+
public DynamicDependencyAttribute(string memberSignature) => MemberSignature = memberSignature;
34+
35+
/// <summary>
36+
/// Initializes a new instance of the <see cref="DynamicDependencyAttribute"/> class
37+
/// with the specified signature of a member on a <see cref="System.Type"/>.
38+
/// </summary>
39+
/// <param name="memberSignature">The signature of the member depended on.</param>
40+
/// <param name="type">The <see cref="System.Type"/> containing <paramref name="memberSignature"/>.</param>
41+
public DynamicDependencyAttribute(string memberSignature, Type type)
42+
{
43+
MemberSignature = memberSignature;
44+
Type = type;
45+
}
46+
47+
/// <summary>
48+
/// Initializes a new instance of the <see cref="DynamicDependencyAttribute"/> class
49+
/// with the specified signature of a member on a type in an assembly.
50+
/// </summary>
51+
/// <param name="memberSignature">The signature of the member depended on.</param>
52+
/// <param name="typeName">The full name of the type containing the specified member.</param>
53+
/// <param name="assemblyName">The assembly name of the type containing the specified member.</param>
54+
public DynamicDependencyAttribute(string memberSignature, string typeName, string assemblyName)
55+
{
56+
MemberSignature = memberSignature;
57+
TypeName = typeName;
58+
AssemblyName = assemblyName;
59+
}
60+
61+
/// <summary>
62+
/// Initializes a new instance of the <see cref="DynamicDependencyAttribute"/> class
63+
/// with the specified types of members on a <see cref="System.Type"/>.
64+
/// </summary>
65+
/// <param name="memberTypes">The types of members depended on.</param>
66+
/// <param name="type">The <see cref="System.Type"/> containing the specified members.</param>
67+
public DynamicDependencyAttribute(DynamicallyAccessedMemberTypes memberTypes, Type type)
68+
{
69+
MemberTypes = memberTypes;
70+
Type = type;
71+
}
72+
73+
/// <summary>
74+
/// Initializes a new instance of the <see cref="DynamicDependencyAttribute"/> class
75+
/// with the specified types of members on a type in an assembly.
76+
/// </summary>
77+
/// <param name="memberTypes">The types of members depended on.</param>
78+
/// <param name="typeName">The full name of the type containing the specified members.</param>
79+
/// <param name="assemblyName">The assembly name of the type containing the specified members.</param>
80+
public DynamicDependencyAttribute(DynamicallyAccessedMemberTypes memberTypes, string typeName, string assemblyName)
81+
{
82+
MemberTypes = memberTypes;
83+
TypeName = typeName;
84+
AssemblyName = assemblyName;
85+
}
86+
87+
/// <summary>
88+
/// Gets the signature of the member depended on.
89+
/// </summary>
90+
/// <remarks>
91+
/// Either <see cref="MemberSignature"/> must be a valid string or <see cref="MemberTypes"/>
92+
/// must not equal <see cref="DynamicallyAccessedMemberTypes.None"/>, but not both.
93+
/// </remarks>
94+
public string? MemberSignature { get; }
95+
96+
/// <summary>
97+
/// Gets the <see cref="DynamicallyAccessedMemberTypes"/> which specifies the type
98+
/// of members depended on.
99+
/// </summary>
100+
/// <remarks>
101+
/// Either <see cref="MemberSignature"/> must be a valid string or <see cref="MemberTypes"/>
102+
/// must not equal <see cref="DynamicallyAccessedMemberTypes.None"/>, but not both.
103+
/// </remarks>
104+
public DynamicallyAccessedMemberTypes MemberTypes { get; }
105+
106+
/// <summary>
107+
/// Gets the <see cref="System.Type"/> containing the specified member.
108+
/// </summary>
109+
/// <remarks>
110+
/// If neither <see cref="Type"/> nor <see cref="TypeName"/> are specified,
111+
/// the type of the consumer is assumed.
112+
/// </remarks>
113+
public Type? Type { get; }
114+
115+
/// <summary>
116+
/// Gets the full name of the type containing the specified member.
117+
/// </summary>
118+
/// <remarks>
119+
/// If neither <see cref="Type"/> nor <see cref="TypeName"/> are specified,
120+
/// the type of the consumer is assumed.
121+
/// </remarks>
122+
public string? TypeName { get; }
123+
124+
/// <summary>
125+
/// Gets the assembly name of the specified type.
126+
/// </summary>
127+
/// <remarks>
128+
/// <see cref="AssemblyName"/> is only valid when <see cref="TypeName"/> is specified.
129+
/// </remarks>
130+
public string? AssemblyName { get; }
131+
132+
/// <summary>
133+
/// Gets or sets the condition in which the dependency is applicable, e.g. "DEBUG".
134+
/// </summary>
135+
public string? Condition { get; set; }
136+
}
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
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+
#pragma warning disable IDE0079
5+
#pragma warning disable CA2217
6+
#pragma warning disable SA1413
7+
#pragma warning disable SA1512
8+
#pragma warning disable S4070
9+
10+
namespace System.Diagnostics.CodeAnalysis;
11+
12+
/// <summary>
13+
/// Specifies the types of members that are dynamically accessed.
14+
///
15+
/// This enumeration has a <see cref="FlagsAttribute"/> attribute that allows a
16+
/// bitwise combination of its member values.
17+
/// </summary>
18+
[Flags]
19+
internal enum DynamicallyAccessedMemberTypes
20+
{
21+
/// <summary>
22+
/// Specifies no members.
23+
/// </summary>
24+
None = 0,
25+
26+
/// <summary>
27+
/// Specifies the default, parameterless public constructor.
28+
/// </summary>
29+
PublicParameterlessConstructor = 0x0001,
30+
31+
/// <summary>
32+
/// Specifies all public constructors.
33+
/// </summary>
34+
PublicConstructors = 0x0002 | PublicParameterlessConstructor,
35+
36+
/// <summary>
37+
/// Specifies all non-public constructors.
38+
/// </summary>
39+
NonPublicConstructors = 0x0004,
40+
41+
/// <summary>
42+
/// Specifies all public methods.
43+
/// </summary>
44+
PublicMethods = 0x0008,
45+
46+
/// <summary>
47+
/// Specifies all non-public methods.
48+
/// </summary>
49+
NonPublicMethods = 0x0010,
50+
51+
/// <summary>
52+
/// Specifies all public fields.
53+
/// </summary>
54+
PublicFields = 0x0020,
55+
56+
/// <summary>
57+
/// Specifies all non-public fields.
58+
/// </summary>
59+
NonPublicFields = 0x0040,
60+
61+
/// <summary>
62+
/// Specifies all public nested types.
63+
/// </summary>
64+
PublicNestedTypes = 0x0080,
65+
66+
/// <summary>
67+
/// Specifies all non-public nested types.
68+
/// </summary>
69+
NonPublicNestedTypes = 0x0100,
70+
71+
/// <summary>
72+
/// Specifies all public properties.
73+
/// </summary>
74+
PublicProperties = 0x0200,
75+
76+
/// <summary>
77+
/// Specifies all non-public properties.
78+
/// </summary>
79+
NonPublicProperties = 0x0400,
80+
81+
/// <summary>
82+
/// Specifies all public events.
83+
/// </summary>
84+
PublicEvents = 0x0800,
85+
86+
/// <summary>
87+
/// Specifies all non-public events.
88+
/// </summary>
89+
NonPublicEvents = 0x1000,
90+
91+
/// <summary>
92+
/// Specifies all interfaces implemented by the type.
93+
/// </summary>
94+
Interfaces = 0x2000,
95+
96+
/// <summary>
97+
/// Specifies all members.
98+
/// </summary>
99+
All = ~None
100+
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
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+
#pragma warning disable IDE0079
5+
#pragma warning disable SA1101
6+
#pragma warning disable SA1116
7+
#pragma warning disable SA1117
8+
#pragma warning disable SA1512
9+
#pragma warning disable SA1623
10+
#pragma warning disable SA1642
11+
#pragma warning disable S3903
12+
13+
namespace System.Diagnostics.CodeAnalysis;
14+
15+
/// <summary>
16+
/// Indicates that certain members on a specified <see cref="Type"/> are accessed dynamically,
17+
/// for example through <see cref="System.Reflection"/>.
18+
/// </summary>
19+
/// <remarks>
20+
/// This allows tools to understand which members are being accessed during the execution
21+
/// of a program.
22+
///
23+
/// This attribute is valid on members whose type is <see cref="Type"/> or <see cref="string"/>.
24+
///
25+
/// When this attribute is applied to a location of type <see cref="string"/>, the assumption is
26+
/// that the string represents a fully qualified type name.
27+
///
28+
/// When this attribute is applied to a class, interface, or struct, the members specified
29+
/// can be accessed dynamically on <see cref="Type"/> instances returned from calling
30+
/// <see cref="object.GetType"/> on instances of that class, interface, or struct.
31+
///
32+
/// If the attribute is applied to a method it's treated as a special case and it implies
33+
/// the attribute should be applied to the "this" parameter of the method. As such the attribute
34+
/// should only be used on instance methods of types assignable to System.Type (or string, but no methods
35+
/// will use it there).
36+
/// </remarks>
37+
[AttributeUsage(
38+
AttributeTargets.Field | AttributeTargets.ReturnValue | AttributeTargets.GenericParameter |
39+
AttributeTargets.Parameter | AttributeTargets.Property | AttributeTargets.Method |
40+
AttributeTargets.Class | AttributeTargets.Interface | AttributeTargets.Struct,
41+
Inherited = false)]
42+
[ExcludeFromCodeCoverage]
43+
internal sealed class DynamicallyAccessedMembersAttribute : Attribute
44+
{
45+
/// <summary>
46+
/// Initializes a new instance of the <see cref="DynamicallyAccessedMembersAttribute"/> class
47+
/// with the specified member types.
48+
/// </summary>
49+
/// <param name="memberTypes">The types of members dynamically accessed.</param>
50+
public DynamicallyAccessedMembersAttribute(DynamicallyAccessedMemberTypes memberTypes) => MemberTypes = memberTypes;
51+
52+
/// <summary>
53+
/// Gets the <see cref="DynamicallyAccessedMemberTypes"/> which specifies the type
54+
/// of members dynamically accessed.
55+
/// </summary>
56+
public DynamicallyAccessedMemberTypes MemberTypes { get; }
57+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
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+
#pragma warning disable IDE0079
5+
#pragma warning disable SA1101
6+
#pragma warning disable SA1116
7+
#pragma warning disable SA1117
8+
#pragma warning disable SA1512
9+
#pragma warning disable SA1623
10+
#pragma warning disable SA1642
11+
#pragma warning disable S3903
12+
#pragma warning disable S3996
13+
14+
namespace System.Diagnostics.CodeAnalysis;
15+
16+
/// <summary>
17+
/// /// Indicates that the specified method requires dynamic access to code that is not referenced
18+
/// statically, for example through <see cref="System.Reflection"/>.
19+
/// </summary>
20+
/// <remarks>
21+
/// This allows tools to understand which methods are unsafe to call when removing unreferenced
22+
/// code from an application.
23+
/// </remarks>
24+
[AttributeUsage(AttributeTargets.Method | AttributeTargets.Constructor | AttributeTargets.Class, Inherited = false)]
25+
[ExcludeFromCodeCoverage]
26+
internal sealed class RequiresUnreferencedCodeAttribute : Attribute
27+
{
28+
/// <summary>
29+
/// Initializes a new instance of the <see cref="RequiresUnreferencedCodeAttribute"/> class
30+
/// with the specified message.
31+
/// </summary>
32+
/// <param name="message">
33+
/// A message that contains information about the usage of unreferenced code.
34+
/// </param>
35+
public RequiresUnreferencedCodeAttribute(string message) => Message = message;
36+
37+
/// <summary>
38+
/// Gets a message that contains information about the usage of unreferenced code.
39+
/// </summary>
40+
public string Message { get; }
41+
42+
/// <summary>
43+
/// Gets or sets an optional URL that contains more information about the method,
44+
/// why it requires unreferenced code, and what options a consumer has to deal with it.
45+
/// </summary>
46+
public string? Url { get; set; }
47+
}

0 commit comments

Comments
 (0)