Skip to content

Commit 4fdb347

Browse files
committed
fix: polyfill DynamicallyAccessedMembers attribute, propagate DAM annotation
1 parent 906b24b commit 4fdb347

4 files changed

Lines changed: 175 additions & 7 deletions

File tree

src/OpenTelemetry.Exporter.OpenTelemetryProtocol/OpenTelemetry.Exporter.OpenTelemetryProtocol.csproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@
3636

3737
<ItemGroup>
3838
<Compile Include="$(RepoRoot)\src\Shared\ConditionalWeakTableExtensions.cs" Link="Includes\ConditionalWeakTableExtensions.cs" />
39+
<Compile Include="$(RepoRoot)\src\Shared\DynamicallyAccessedMembersAttribute.cs" Link="Includes\DynamicallyAccessedMembersAttribute.cs" />
40+
<Compile Include="$(RepoRoot)\src\Shared\DynamicallyAccessedMemberTypes.cs" Link="Includes\DynamicallyAccessedMemberTypes.cs" />
3941
<Compile Include="$(RepoRoot)\src\Shared\EncodingExtensions.cs" Link="Includes\EncodingExtensions.cs" />
4042
<Compile Include="$(RepoRoot)\src\Shared\HttpClientHelpers.cs" Link="Includes\HttpClientHelpers.cs" />
4143
<Compile Include="$(RepoRoot)\src\Shared\PeriodicExportingMetricReaderHelper.cs" Link="Includes\PeriodicExportingMetricReaderHelper.cs" />

src/Shared/ConditionalWeakTableExtensions.cs

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,14 @@ namespace System.Runtime.CompilerServices;
1313
/// </summary>
1414
internal static class ConditionalWeakTableExtensions
1515
{
16-
#pragma warning disable SA1101 // Prefix local calls with this - extension receiver is the parameter, not 'this'.
17-
extension<TKey, [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicParameterlessConstructor)] TValue>(ConditionalWeakTable<TKey, TValue> table)
16+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
17+
public static TValue GetOrAdd<TKey, [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicParameterlessConstructor)] TValue>(
18+
this ConditionalWeakTable<TKey, TValue> table,
19+
TKey key,
20+
ConditionalWeakTable<TKey, TValue>.CreateValueCallback valueFactory)
1821
where TKey : class
1922
where TValue : class
20-
{
21-
[MethodImpl(MethodImplOptions.AggressiveInlining)]
22-
public TValue GetOrAdd(TKey key, ConditionalWeakTable<TKey, TValue>.CreateValueCallback valueFactory) => table.GetValue(key, valueFactory);
23-
}
24-
#pragma warning restore SA1101
23+
=> table.GetValue(key, valueFactory);
2524
}
2625

2726
#endif
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
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+
#if NETFRAMEWORK || NETSTANDARD2_0
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="global::System.FlagsAttribute"/> attribute that allows a
16+
/// bitwise combination of its member values.
17+
/// </summary>
18+
[global::System.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 | global::System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.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 = ~global::System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.None
100+
}
101+
102+
#endif
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
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+
#if NETFRAMEWORK || NETSTANDARD2_0
9+
10+
namespace System.Diagnostics.CodeAnalysis;
11+
12+
/// <summary>
13+
/// Indicates that certain members on a specified <see cref="global::System.Type"/> are accessed dynamically,
14+
/// for example through <see cref="global::System.Reflection"/>.
15+
/// </summary>
16+
/// <remarks>
17+
/// This allows tools to understand which members are being accessed during the execution
18+
/// of a program.
19+
///
20+
/// This attribute is valid on members whose type is <see cref="global::System.Type"/> or <see cref="string"/>.
21+
///
22+
/// When this attribute is applied to a location of type <see cref="string"/>, the assumption is
23+
/// that the string represents a fully qualified type name.
24+
///
25+
/// When this attribute is applied to a class, interface, or struct, the members specified
26+
/// can be accessed dynamically on <see cref="global::System.Type"/> instances returned from calling
27+
/// <see cref="object.GetType"/> on instances of that class, interface, or struct.
28+
///
29+
/// If the attribute is applied to a method it's treated as a special case and it implies
30+
/// the attribute should be applied to the "this" parameter of the method. As such the attribute
31+
/// should only be used on instance methods of types assignable to System.Type (or string, but no methods
32+
/// will use it there).
33+
/// </remarks>
34+
[global::System.AttributeUsage(
35+
global::System.AttributeTargets.Field |
36+
global::System.AttributeTargets.ReturnValue |
37+
global::System.AttributeTargets.GenericParameter |
38+
global::System.AttributeTargets.Parameter |
39+
global::System.AttributeTargets.Property |
40+
global::System.AttributeTargets.Method |
41+
global::System.AttributeTargets.Class |
42+
global::System.AttributeTargets.Interface |
43+
global::System.AttributeTargets.Struct,
44+
Inherited = false)]
45+
[global::System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage]
46+
internal sealed class DynamicallyAccessedMembersAttribute : global::System.Attribute
47+
{
48+
/// <summary>
49+
/// Initializes a new instance of the <see cref="global::System.Diagnostics.CodeAnalysis.DynamicallyAccessedMembersAttribute"/> class
50+
/// with the specified member types.
51+
/// </summary>
52+
/// <param name="memberTypes">The types of members dynamically accessed.</param>
53+
public DynamicallyAccessedMembersAttribute(global::System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes memberTypes)
54+
{
55+
MemberTypes = memberTypes;
56+
}
57+
58+
/// <summary>
59+
/// Gets the <see cref="global::System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes"/> which specifies the type
60+
/// of members dynamically accessed.
61+
/// </summary>
62+
public global::System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes MemberTypes { get; }
63+
}
64+
65+
#endif

0 commit comments

Comments
 (0)