CommandContextParameter.cs 864 B

123456789101112131415161718192021222324252627
  1. using Ninject.Activation;
  2. using Ninject.Parameters;
  3. using System;
  4. using System.CommandLine.Invocation;
  5. using System.Linq;
  6. namespace CustomHostingDemo
  7. {
  8. public class CommandContextParameter : Parameter
  9. {
  10. public InvocationContext Context { get; }
  11. public IDefaultArgs Args { get; set; }
  12. public CommandContextParameter(InvocationContext context, IDefaultArgs args)
  13. : base(nameof(CommandContextParameter), context, shouldInherit: true)
  14. {
  15. Context = context;
  16. Args = args;
  17. }
  18. public static CommandContextParameter FromContext(IContext context)
  19. {
  20. return context.Parameters.OfType<CommandContextParameter>().FirstOrDefault()
  21. ?? throw new Exception("Cannot resolve InvocationContext outside of the scope of an invocation");
  22. }
  23. }
  24. }