Skip to content

Commit

Permalink
Support open delegate for Nullable<> (#42837)
Browse files Browse the repository at this point in the history
* Support open delegate for Nullable<>

* Disable new tests on Mono

Co-authored-by: David Wrighton <davidwr@microsoft.com>
  • Loading branch information
amos402 and davidwrighton authored Mar 19, 2021
1 parent fc80be1 commit ef6c694
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 5 deletions.
5 changes: 0 additions & 5 deletions src/coreclr/vm/comdelegate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
24 changes: 24 additions & 0 deletions src/libraries/System.Runtime/tests/System/DelegateTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1101,6 +1101,28 @@ public static void CreateDelegate9_Type_Null()
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
Expand Down Expand Up @@ -1207,6 +1229,8 @@ public interface Iface

public delegate void D(C c);
public delegate int E(C c);

delegate string NullableIntToString(ref int? obj);
#endregion Test Setup
}
}
Expand Down

0 comments on commit ef6c694

Please sign in to comment.