From e284d9d74c1ea5ac1c1ce295d243d29b9ff1b3cc Mon Sep 17 00:00:00 2001 From: =?utf8?q?Michal=20Strehovsk=C3=BD?= Date: Wed, 17 Jun 2020 14:00:14 +0200 Subject: [PATCH] Annotate delegate APIs for trimming friendliness (#37964) --- src/coreclr/src/System.Private.CoreLib/src/System/Delegate.CoreCLR.cs | 4 ++++ src/libraries/System.Private.CoreLib/src/System/Delegate.cs | 4 ++++ src/mono/netcore/System.Private.CoreLib/src/System/Delegate.Mono.cs | 4 ++++ 3 files changed, 12 insertions(+) diff --git a/src/coreclr/src/System.Private.CoreLib/src/System/Delegate.CoreCLR.cs b/src/coreclr/src/System.Private.CoreLib/src/System/Delegate.CoreCLR.cs index 1ff1b7b..c947666 100644 --- a/src/coreclr/src/System.Private.CoreLib/src/System/Delegate.CoreCLR.cs +++ b/src/coreclr/src/System.Private.CoreLib/src/System/Delegate.CoreCLR.cs @@ -3,6 +3,7 @@ // See the LICENSE file in the project root for more information. using System.Diagnostics; +using System.Diagnostics.CodeAnalysis; using System.Reflection; using System.Runtime.CompilerServices; using System.Runtime.InteropServices; @@ -218,6 +219,7 @@ namespace System public object? Target => GetTarget(); // V1 API. + [RequiresUnreferencedCode("The target method might be removed")] public static Delegate? CreateDelegate(Type type, object target, string method, bool ignoreCase, bool throwOnBindFailure) { if (type == null) @@ -256,6 +258,7 @@ namespace System } // V1 API. + [RequiresUnreferencedCode("The target method might be removed")] public static Delegate? CreateDelegate(Type type, Type target, string method, bool ignoreCase, bool throwOnBindFailure) { if (type == null) @@ -416,6 +419,7 @@ namespace System // internal implementation details (FCALLS and utilities) // + [RequiresUnreferencedCode("The target method might be removed")] [MethodImpl(MethodImplOptions.InternalCall)] private extern bool BindToMethodName(object? target, RuntimeType methodType, string method, DelegateBindingFlags flags); diff --git a/src/libraries/System.Private.CoreLib/src/System/Delegate.cs b/src/libraries/System.Private.CoreLib/src/System/Delegate.cs index 5502fc8..fa44e19 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Delegate.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Delegate.cs @@ -42,11 +42,15 @@ namespace System public static Delegate CreateDelegate(Type type, MethodInfo method) => CreateDelegate(type, method, throwOnBindFailure: true)!; // V1 api: Creates closed delegates to instance methods only, relaxed signature checking disallowed. + [RequiresUnreferencedCode("The target method might be removed")] public static Delegate CreateDelegate(Type type, object target, string method) => CreateDelegate(type, target, method, ignoreCase: false, throwOnBindFailure: true)!; + [RequiresUnreferencedCode("The target method might be removed")] public static Delegate CreateDelegate(Type type, object target, string method, bool ignoreCase) => CreateDelegate(type, target, method, ignoreCase, throwOnBindFailure: true)!; // V1 api: Creates open delegates to static methods only, relaxed signature checking disallowed. + [RequiresUnreferencedCode("The target method might be removed")] public static Delegate CreateDelegate(Type type, Type target, string method) => CreateDelegate(type, target, method, ignoreCase: false, throwOnBindFailure: true)!; + [RequiresUnreferencedCode("The target method might be removed")] public static Delegate CreateDelegate(Type type, Type target, string method, bool ignoreCase) => CreateDelegate(type, target, method, ignoreCase, throwOnBindFailure: true)!; #if !CORERT diff --git a/src/mono/netcore/System.Private.CoreLib/src/System/Delegate.Mono.cs b/src/mono/netcore/System.Private.CoreLib/src/System/Delegate.Mono.cs index 6e991de..2aaeb0c 100644 --- a/src/mono/netcore/System.Private.CoreLib/src/System/Delegate.Mono.cs +++ b/src/mono/netcore/System.Private.CoreLib/src/System/Delegate.Mono.cs @@ -29,6 +29,7 @@ // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. // +using System.Diagnostics.CodeAnalysis; using System.Reflection; using System.Runtime.CompilerServices; using System.Runtime.InteropServices; @@ -150,6 +151,7 @@ namespace System return d; } + [RequiresUnreferencedCode("The target method might be removed")] public static Delegate? CreateDelegate(Type type, object target, string method, bool ignoreCase, bool throwOnBindFailure) { if (type is null) @@ -176,6 +178,7 @@ namespace System return CreateDelegate_internal(type, null, info, throwOnBindFailure); } + [RequiresUnreferencedCode("The target method might be removed")] public static Delegate? CreateDelegate(Type type, Type target, string method, bool ignoreCase, bool throwOnBindFailure) { if (type is null) @@ -207,6 +210,7 @@ namespace System return CreateDelegate_internal(type, null, info, throwOnBindFailure); } + [RequiresUnreferencedCode("The target method might be removed")] private static MethodInfo? GetCandidateMethod(Type type, Type target, string method, BindingFlags bflags, bool ignoreCase) { MethodInfo? invoke = type.GetMethod("Invoke"); -- 2.7.4