Skip to content

Commit

Permalink
using ReflectedType instead of DeclaringType
Browse files Browse the repository at this point in the history
in ModelBindingCommandHandler and MethodInfoHandlerDescriptor
as suggested by aayjaychan
  • Loading branch information
eisbaer66 authored and jonsequitur committed Apr 19, 2022
1 parent 18069a6 commit ce54d76
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 2 deletions.
79 changes: 79 additions & 0 deletions src/System.CommandLine.Hosting.Tests/HostingHandlerTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,50 @@ public static async Task Can_bind_to_arguments_via_injection()
service.StringValue.Should().Be("TEST");
}

[Fact]
public static async Task Invokes_DerivedClass()
{
var service = new MyService();

var cmd = new RootCommand();
cmd.AddCommand(new MyCommand());
cmd.AddCommand(new MyOtherCommand());
var parser = new CommandLineBuilder(cmd)
.UseHost((builder) => {
builder.ConfigureServices(services =>
{
services.AddTransient(x => service);
})
.UseCommandHandler<MyCommand, MyCommand.MyDerivedHandler>()
.UseCommandHandler<MyOtherCommand, MyOtherCommand.MyDerivedHandler>();
})
.Build();

await parser.InvokeAsync(new string[] { "mycommand", "--int-option", "54" });
service.Value.Should().Be(54);

await parser.InvokeAsync(new string[] { "myothercommand", "TEST" });
service.StringValue.Should().Be("TEST");
}

public abstract class MyBaseHandler : ICommandHandler
{
public int IntOption { get; set; } // bound from option
public IConsole Console { get; set; } // bound from DI

public int Invoke(InvocationContext context)
{
return Act();
}

public Task<int> InvokeAsync(InvocationContext context)
{
return Task.FromResult(Act());
}

protected abstract int Act();
}

public class MyCommand : Command
{
public MyCommand() : base(name: "mycommand")
Expand Down Expand Up @@ -144,6 +188,22 @@ public Task<int> InvokeAsync(InvocationContext context)
return Task.FromResult(IntOption);
}
}

public class MyDerivedHandler : MyBaseHandler
{
private readonly MyService service;

public MyDerivedHandler(MyService service)
{
this.service = service;
}

protected override int Act()
{
service.Value = IntOption;
return IntOption;
}
}
}

public class MyOtherCommand : Command
Expand Down Expand Up @@ -177,6 +237,25 @@ public Task<int> InvokeAsync(InvocationContext context)
return Task.FromResult(service.Action?.Invoke() ?? 0);
}
}

public class MyDerivedHandler : MyBaseHandler
{
private readonly MyService service;

public MyDerivedHandler(MyService service)
{
this.service = service;
}

public string One { get; set; }

protected override int Act()
{
service.Value = IntOption;
service.StringValue = One;
return service.Action?.Invoke() ?? 0;
}
}
}

public class MyService
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public override ICommandHandler GetCommandHandler()
}
}

public override ModelDescriptor Parent => ModelDescriptor.FromType(_handlerMethodInfo.DeclaringType);
public override ModelDescriptor Parent => ModelDescriptor.FromType(_handlerMethodInfo.ReflectedType);

private protected override IEnumerable<ParameterDescriptor> InitializeParameterDescriptors() =>
_handlerMethodInfo.GetParameters()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ public async Task<int> InvokeAsync(InvocationContext context)
if (_handlerDelegate is null)
{
var invocationTarget = _invocationTarget ??
bindingContext.GetService(_handlerMethodInfo!.DeclaringType);
bindingContext.GetService(_handlerMethodInfo!.ReflectedType);
if(invocationTarget is { })
{
_invocationTargetBinder?.UpdateInstance(invocationTarget, bindingContext);
Expand Down

0 comments on commit ce54d76

Please sign in to comment.