From fe41d3c762695cfeb364a8a0b61e2e376d22da5a Mon Sep 17 00:00:00 2001 From: Vlad Brezae Date: Wed, 5 Aug 2020 23:04:00 +0300 Subject: [PATCH] [mono] Pass delegate target when using CreateDelegate (#40321) * Pass delegate target when using CreateDelegate * [tests] Add test that checks `this` with CreateDelegate API --- .../System.Runtime/tests/System/DelegateTests.cs | 10 ++++++++++ .../System.Private.CoreLib/src/System/Delegate.Mono.cs | 2 +- 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/src/libraries/System.Runtime/tests/System/DelegateTests.cs b/src/libraries/System.Runtime/tests/System/DelegateTests.cs index 35ee59b921132..055fd8274e266 100644 --- a/src/libraries/System.Runtime/tests/System/DelegateTests.cs +++ b/src/libraries/System.Runtime/tests/System/DelegateTests.cs @@ -619,6 +619,10 @@ public static void CreateDelegate2() e = (E)Delegate.CreateDelegate(typeof(E), new C(), "DoExecute"); Assert.NotNull(e); Assert.Equal(102, e(new C())); + + e = (E)Delegate.CreateDelegate(typeof(E), new B() { field = 42 }, "GetField"); + Assert.NotNull(e); + Assert.Equal(42, e(new C())); } [Fact] @@ -1103,6 +1107,7 @@ public static void CreateDelegate9_Type_Null() public class B { + public int field; public virtual string retarg3(string s) { @@ -1136,6 +1141,11 @@ public int StartExecute(C c, B b) { return 3; } + + public int GetField(C c) + { + return field; + } } public class C : B, Iface 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 af5a59415591a..faf349979a6a8 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 @@ -177,7 +177,7 @@ public static Delegate CreateDelegate(Type type, object? firstArgument, MethodIn return null; } - return CreateDelegate_internal(type, null, info, throwOnBindFailure); + return CreateDelegate_internal(type, target, info, throwOnBindFailure); } [RequiresUnreferencedCode("The target method might be removed")]