diff --git a/Refit/DynamicallyAccessedMembersAttribute.cs b/Refit/DynamicallyAccessedMembersAttribute.cs new file mode 100644 index 000000000..d474aafb3 --- /dev/null +++ b/Refit/DynamicallyAccessedMembersAttribute.cs @@ -0,0 +1,65 @@ +#if NETSTANDARD2_0 || NET462 +namespace System.Diagnostics.CodeAnalysis; + +/// +/// Indicates that certain members on a specified are accessed dynamically, +/// for example through . +/// +/// +/// This allows tools to understand which members are being accessed during the execution +/// of a program. +/// +/// This attribute is valid on members whose type is or . +/// +/// When this attribute is applied to a location of type , the assumption is +/// that the string represents a fully qualified type name. +/// +/// When this attribute is applied to a class, interface, or struct, the members specified +/// can be accessed dynamically on instances returned from calling +/// on instances of that class, interface, or struct. +/// +/// If the attribute is applied to a method it's treated as a special case and it implies +/// the attribute should be applied to the "this" parameter of the method. As such the attribute +/// should only be used on instance methods of types assignable to System.Type (or string, but no methods +/// will use it there). +/// +internal sealed class DynamicallyAccessedMembersAttribute : Attribute +{ + /// + /// Initializes a new instance of the class + /// with the specified member types. + /// + /// The types of members dynamically accessed. + public DynamicallyAccessedMembersAttribute(DynamicallyAccessedMemberTypes memberTypes) + { + MemberTypes = memberTypes; + } + + /// + /// Gets the which specifies the type + /// of members dynamically accessed. + /// + public DynamicallyAccessedMemberTypes MemberTypes { get; } +} + +[Flags] +internal enum DynamicallyAccessedMemberTypes +{ + None = 0, + PublicParameterlessConstructor = 1, + PublicConstructors = 3, + NonPublicConstructors = 4, + PublicMethods = 8, + NonPublicMethods = 16, // 0x00000010 + PublicFields = 32, // 0x00000020 + NonPublicFields = 64, // 0x00000040 + PublicNestedTypes = 128, // 0x00000080 + NonPublicNestedTypes = 256, // 0x00000100 + PublicProperties = 512, // 0x00000200 + NonPublicProperties = 1024, // 0x00000400 + PublicEvents = 2048, // 0x00000800 + NonPublicEvents = 4096, // 0x00001000 + Interfaces = 8192, // 0x00002000 + All = -1, // 0xFFFFFFFF +} +#endif diff --git a/Refit/RequestBuilderImplementation.cs b/Refit/RequestBuilderImplementation.cs index 0ae00867f..520ca512a 100644 --- a/Refit/RequestBuilderImplementation.cs +++ b/Refit/RequestBuilderImplementation.cs @@ -1,6 +1,7 @@ using System.Collections; using System.Collections.Concurrent; using System.Diagnostics; +using System.Diagnostics.CodeAnalysis; using System.Net.Http; using System.Reflection; using System.Text; @@ -57,7 +58,7 @@ public RequestBuilderImplementation( } void AddInterfaceHttpMethods( - Type interfaceType, + [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicMethods)]Type interfaceType, Dictionary> methods ) {