Support open delegate for Nullable<> (#42837)
authoramos402 <amos_402@msn.com>
Fri, 19 Mar 2021 18:35:02 +0000 (02:35 +0800)
committerGitHub <noreply@github.com>
Fri, 19 Mar 2021 18:35:02 +0000 (11:35 -0700)
* Support open delegate for Nullable<>

* Disable new tests on Mono

Co-authored-by: David Wrighton <davidwr@microsoft.com>
src/coreclr/vm/comdelegate.cpp
src/libraries/System.Runtime/tests/System/DelegateTests.cs

index abee693..a500c76 100644 (file)
@@ -2600,11 +2600,6 @@ bool COMDelegate::IsMethodDescCompatible(TypeHandle   thFirstArg,
     if (flags & DBF_InstanceMethodOnly && pTargetMethod->IsStatic())
         return false;
 
-    // we don't allow you to bind to methods on Nullable<T> because the unboxing stubs don't know how to
-    // handle this case.
-    if (!pTargetMethod->IsStatic() && Nullable::IsNullableType(pTargetMethod->GetMethodTable()))
-        return false;
-
     // Get signatures for the delegate invoke and target methods.
     MetaSig sigInvoke(pInvokeMethod, thDelegate);
     MetaSig sigTarget(pTargetMethod, thExactMethodType);
index 055fd82..cadeabb 100644 (file)
@@ -1101,6 +1101,28 @@ namespace System.Tests
             Assert.Null(ex.InnerException);
             Assert.NotNull(ex.Message);
         }
+
+        [ActiveIssue("https://github.com/dotnet/runtime/issues/49839", TestRuntimes.Mono)]
+        [Fact]
+        public static void CreateDelegate10_Nullable_Method()
+        {
+            int? num = 123;
+            MethodInfo mi = typeof(int?).GetMethod("ToString");
+            NullableIntToString toString = (NullableIntToString)Delegate.CreateDelegate(
+                typeof(NullableIntToString), mi);
+            string s = toString(ref num);
+            Assert.Equal(num.ToString(), s);
+        }
+
+        [ActiveIssue("https://github.com/dotnet/runtime/issues/49839", TestRuntimes.Mono)]
+        [Fact]
+        public static void CreateDelegate10_Nullable_ClosedDelegate()
+        {
+            int? num = 123;
+            MethodInfo mi = typeof(int?).GetMethod("ToString");
+            AssertExtensions.Throws<ArgumentException>(
+                () => Delegate.CreateDelegate(typeof(NullableIntToString), num, mi));
+        }
         #endregion Tests
 
         #region Test Setup
@@ -1207,6 +1229,8 @@ namespace System.Tests
 
         public delegate void D(C c);
         public delegate int E(C c);
+
+        delegate string NullableIntToString(ref int? obj);
         #endregion Test Setup
     }
 }