[mono] Pass delegate target when using CreateDelegate (#40321)
authorVlad Brezae <brezaevlad@gmail.com>
Wed, 5 Aug 2020 20:04:00 +0000 (23:04 +0300)
committerGitHub <noreply@github.com>
Wed, 5 Aug 2020 20:04:00 +0000 (23:04 +0300)
* Pass delegate target when using CreateDelegate

* [tests] Add test that checks `this` with CreateDelegate API

src/libraries/System.Runtime/tests/System/DelegateTests.cs
src/mono/netcore/System.Private.CoreLib/src/System/Delegate.Mono.cs

index 35ee59b..055fd82 100644 (file)
@@ -619,6 +619,10 @@ namespace System.Tests
             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 @@ namespace System.Tests
 
         public class B
         {
+            public int field;
 
             public virtual string retarg3(string s)
             {
@@ -1136,6 +1141,11 @@ namespace System.Tests
             {
                 return 3;
             }
+
+            public int GetField(C c)
+            {
+                return field;
+            }
         }
 
         public class C : B, Iface
index af5a594..faf3499 100644 (file)
@@ -177,7 +177,7 @@ namespace System
                 return null;
             }
 
-            return CreateDelegate_internal(type, null, info, throwOnBindFailure);
+            return CreateDelegate_internal(type, target, info, throwOnBindFailure);
         }
 
         [RequiresUnreferencedCode("The target method might be removed")]