Skip to content

Commit e2815cc

Browse files
ycrumeyrollejonsequitur
authored andcommitted
Use ReflectedTypeinstead of DeclaringType for finding the correct invocation method.
1 parent fd3cf0c commit e2815cc

File tree

2 files changed

+43
-1
lines changed

2 files changed

+43
-1
lines changed

src/System.CommandLine.Tests/Invocation/CommandHandlerTests.cs

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -537,5 +537,47 @@ void Execute(string fullnameOrNickname, int age)
537537
boundName.Should().Be("Gandalf");
538538
boundAge.Should().Be(425);
539539
}
540+
541+
[Theory]
542+
[InlineData(typeof(ConcreteTestCommandHandler), 42)]
543+
[InlineData(typeof(VirtualTestCommandHandler), 42)]
544+
[InlineData(typeof(OverridenVirtualTestCommandHandler), 41)]
545+
public async Task Method_invoked_is_matching_to_the_interface_implementation(Type type, int expectedResult)
546+
{
547+
var command = new Command("command");
548+
command.Handler = CommandHandler.Create(type.GetMethod(nameof(ICommandHandler.InvokeAsync)));
549+
550+
var parser = new Parser(command);
551+
552+
int result = await parser.InvokeAsync("command", _console);
553+
554+
result.Should().Be(expectedResult);
555+
}
556+
557+
public abstract class AbstractTestCommandHandler : ICommandHandler
558+
{
559+
public abstract Task<int> DoJobAsync();
560+
561+
public Task<int> InvokeAsync(InvocationContext context)
562+
=> DoJobAsync();
563+
}
564+
565+
public sealed class ConcreteTestCommandHandler : AbstractTestCommandHandler
566+
{
567+
public override Task<int> DoJobAsync()
568+
=> Task.FromResult(42);
569+
}
570+
571+
public class VirtualTestCommandHandler : ICommandHandler
572+
{
573+
public virtual Task<int> InvokeAsync(InvocationContext context)
574+
=> Task.FromResult(42);
575+
}
576+
577+
public class OverridenVirtualTestCommandHandler : VirtualTestCommandHandler
578+
{
579+
public override Task<int> InvokeAsync(InvocationContext context)
580+
=> Task.FromResult(41);
581+
}
540582
}
541583
}

src/System.CommandLine/Invocation/ModelBindingCommandHandler.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public ModelBindingCommandHandler(
2727
_handlerMethodInfo = handlerMethodInfo ?? throw new ArgumentNullException(nameof(handlerMethodInfo));
2828
_invocationTargetBinder = _handlerMethodInfo.IsStatic
2929
? null
30-
: new ModelBinder(_handlerMethodInfo.DeclaringType);
30+
: new ModelBinder(_handlerMethodInfo.ReflectedType);
3131
_methodDescriptor = methodDescriptor ?? throw new ArgumentNullException(nameof(methodDescriptor));
3232
}
3333

0 commit comments

Comments
 (0)