Skip to content

Commit 018c289

Browse files
authored
Merge pull request #3 from Sergio0694/dev/optional-runtime-attributes
Add optional runtime supported attributes
2 parents a60a03a + 55ef219 commit 018c289

21 files changed

+861
-7
lines changed

README.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,26 @@
3535

3636
To leverage them, make sure to bump your C# language version. You can do this by setting the `<LangVersion>` MSBuild property in your project. For instance, by adding `<LangVersion>11.0</LangVersion>` (or your desired C# version) to the first `<PropertyGroup>` of your .csproj file. For more info on this, [see here](https://sergiopedri.medium.com/enabling-and-using-c-9-features-on-older-and-unsupported-runtimes-ce384d8debb), but remember that you don't need to manually copy polyfills anymore: simply adding a reference to **PolySharp** will do this for you automatically.
3737

38+
It also includes the following optional runtime-supported polyfills:
39+
- Reflection annotation attributes (see [docs](https://learn.microsoft.com/dotnet/core/deploying/trimming/prepare-libraries-for-trimming)):
40+
- `[DynamicallyAccessedMembers]`
41+
- `[DynamicDependency]`
42+
- `[RequiresUnreferencedCode]`
43+
- `[UnconditionalSuppressMessage]`
44+
- `[StackTraceHidden]` (see [here](https://makolyte.com/csharp-exclude-exception-throw-helper-methods-from-the-stack-trace/))
45+
- `[UnmanagedCallersOnly]` (see [docs](https://learn.microsoft.com/dotnet/api/system.runtime.interopservices.unmanagedcallersonlyattribute)))
46+
- Platform support annotation attributes (see [docs](https://learn.microsoft.com/dotnet/standard/analyzers/platform-compat-analyzer)):
47+
- `[ObsoletedOSPlatform]`
48+
- `[SupportedOSPlatform]`
49+
- `[SupportedOSPlatformGuard]`
50+
- `[TargetPlatform]`
51+
- `[UnsupportedOSPlatform]`
52+
- `[UnsupportedOSPlatformGuard]`
53+
3854
# Options ⚙️
3955

4056
**PolySharp**'s generation can be configured through some MSBuild properties to set in consuming projects.
4157

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

0 commit comments

Comments
 (0)