Skip to content

Commit 4bbe565

Browse files
feat: add DynamicallyAccessedMembers attribute (#1973)
Co-authored-by: Chris Pulman <[email protected]>
1 parent 61f2973 commit 4bbe565

File tree

2 files changed

+67
-1
lines changed

2 files changed

+67
-1
lines changed
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
#if NETSTANDARD2_0 || NET462
2+
namespace System.Diagnostics.CodeAnalysis;
3+
4+
/// <summary>
5+
/// Indicates that certain members on a specified <see cref="Type"/> are accessed dynamically,
6+
/// for example through <see cref="Reflection"/>.
7+
/// </summary>
8+
/// <remarks>
9+
/// This allows tools to understand which members are being accessed during the execution
10+
/// of a program.
11+
///
12+
/// This attribute is valid on members whose type is <see cref="Type"/> or <see cref="string"/>.
13+
///
14+
/// When this attribute is applied to a location of type <see cref="string"/>, the assumption is
15+
/// that the string represents a fully qualified type name.
16+
///
17+
/// When this attribute is applied to a class, interface, or struct, the members specified
18+
/// can be accessed dynamically on <see cref="Type"/> instances returned from calling
19+
/// <see cref="object.GetType"/> on instances of that class, interface, or struct.
20+
///
21+
/// If the attribute is applied to a method it's treated as a special case and it implies
22+
/// the attribute should be applied to the "this" parameter of the method. As such the attribute
23+
/// should only be used on instance methods of types assignable to System.Type (or string, but no methods
24+
/// will use it there).
25+
/// </remarks>
26+
internal sealed class DynamicallyAccessedMembersAttribute : Attribute
27+
{
28+
/// <summary>
29+
/// Initializes a new instance of the <see cref="DynamicallyAccessedMembersAttribute"/> class
30+
/// with the specified member types.
31+
/// </summary>
32+
/// <param name="memberTypes">The types of members dynamically accessed.</param>
33+
public DynamicallyAccessedMembersAttribute(DynamicallyAccessedMemberTypes memberTypes)
34+
{
35+
MemberTypes = memberTypes;
36+
}
37+
38+
/// <summary>
39+
/// Gets the <see cref="DynamicallyAccessedMemberTypes"/> which specifies the type
40+
/// of members dynamically accessed.
41+
/// </summary>
42+
public DynamicallyAccessedMemberTypes MemberTypes { get; }
43+
}
44+
45+
[Flags]
46+
internal enum DynamicallyAccessedMemberTypes
47+
{
48+
None = 0,
49+
PublicParameterlessConstructor = 1,
50+
PublicConstructors = 3,
51+
NonPublicConstructors = 4,
52+
PublicMethods = 8,
53+
NonPublicMethods = 16, // 0x00000010
54+
PublicFields = 32, // 0x00000020
55+
NonPublicFields = 64, // 0x00000040
56+
PublicNestedTypes = 128, // 0x00000080
57+
NonPublicNestedTypes = 256, // 0x00000100
58+
PublicProperties = 512, // 0x00000200
59+
NonPublicProperties = 1024, // 0x00000400
60+
PublicEvents = 2048, // 0x00000800
61+
NonPublicEvents = 4096, // 0x00001000
62+
Interfaces = 8192, // 0x00002000
63+
All = -1, // 0xFFFFFFFF
64+
}
65+
#endif

Refit/RequestBuilderImplementation.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System.Collections;
22
using System.Collections.Concurrent;
33
using System.Diagnostics;
4+
using System.Diagnostics.CodeAnalysis;
45
using System.Net.Http;
56
using System.Reflection;
67
using System.Text;
@@ -57,7 +58,7 @@ public RequestBuilderImplementation(
5758
}
5859

5960
void AddInterfaceHttpMethods(
60-
Type interfaceType,
61+
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicMethods)]Type interfaceType,
6162
Dictionary<string, List<RestMethodInfoInternal>> methods
6263
)
6364
{

0 commit comments

Comments
 (0)