Skip to content

Commit

Permalink
Allow open delegates to nullables
Browse files Browse the repository at this point in the history
Port of dotnet/runtime#42837.

Won't win any perf prizes, but will do for now. I don't think the amount of work required for good support will ever meet the bar.
  • Loading branch information
MichalStrehovsky committed Mar 25, 2021
1 parent bbaa3ec commit 668a784
Showing 1 changed file with 24 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,30 @@ public sealed override Delegate CreateDelegate(RuntimeTypeHandle delegateType, o
{
if (_thisType.IsConstructedGenericType && _thisType.GetGenericTypeDefinition() == typeof(Nullable<>))
{
// Desktop compat: MethodInfos to Nullable<T> methods cannot be turned into delegates.
throw new ArgumentException(SR.Arg_DlgtTargMeth);
if (isOpen)
{
return DynamicDelegateAugments.CreateObjectArrayDelegate(Type.GetTypeFromHandle(delegateType),
(args) =>
{
object[] arguments;
if (args.Length > 1)
{
arguments = new object[args.Length - 1];
Array.Copy(args, 1, arguments, 0, args.Length - 1);
}
else
{
arguments = Array.Empty<object>();
}

return _action(args[0], arguments, _thisType);
});
}
else
{
// Desktop compat: MethodInfos to Nullable<T> methods cannot be turned into delegates.
throw new ArgumentException(SR.Arg_DlgtTargMeth);
}
}

throw new PlatformNotSupportedException();
Expand Down

0 comments on commit 668a784

Please sign in to comment.