Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support open delegate for Nullable<> #42837

Merged
merged 2 commits into from
Mar 19, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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