private void AddSubcommandImpl <TSubCommand>(SubcommandAttribute subcommand) where TSubCommand : class, new() { if (App.Commands.Any(c => c.Name.Equals(subcommand.Name, StringComparison.OrdinalIgnoreCase))) { throw new InvalidOperationException(Strings.DuplicateSubcommandName(subcommand.Name)); } var childApp = App.Command(subcommand.Name, subcommand.Configure); var builder = new ReflectionAppBuilder <TSubCommand>(childApp); builder._bindResult = _bindResult; builder.Initialize(); var subcommandProp = typeof(TTarget).GetTypeInfo().GetProperty("Subcommand", BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public); if (subcommandProp != null) { var setter = ReflectionHelper.GetPropertySetter(subcommandProp); builder.OnBind(o => setter.Invoke(this._target, o)); } var parentProp = subcommand.CommandType.GetTypeInfo().GetProperty("Parent", BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public); if (parentProp != null) { var setter = ReflectionHelper.GetPropertySetter(parentProp); builder.OnBind(o => setter.Invoke(o, this._target)); } }
private void AddSubcommand(SubcommandAttribute subcommand) { var impl = AddSubcommandMethod.MakeGenericMethod(subcommand.CommandType); try { impl.Invoke(this, new object[] { subcommand }); } catch (TargetInvocationException ex) { // unwrap throw ex.InnerException ?? ex; } }
private void AddSubcommandImpl <TSubCommand>(Type parent, SubcommandAttribute subcommand) where TSubCommand : class, new() { var parentApp = App; var childApp = App.Command(subcommand.Name, subcommand.Configure); var builder = new ReflectionAppBuilder <TSubCommand>(childApp); builder.Initialize(); var subcommandProp = parent.GetTypeInfo().GetProperty("Subcommand", BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public); if (subcommandProp != null) { var setter = ReflectionHelper.GetPropertySetter(subcommandProp); builder.OnBind(o => { if (parentApp.State is BindContext ctx) { setter.Invoke(ctx.Target, o); } }); } var parentProp = subcommand.CommandType.GetTypeInfo().GetProperty("Parent", BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public); if (parentProp != null) { var setter = ReflectionHelper.GetPropertySetter(parentProp); builder.OnBind(o => { if (parentApp.State is BindContext ctx) { setter.Invoke(o, ctx.Target); } }); } }
private void AddSubcommand(Type parent, SubcommandAttribute subcommand) { var impl = AddSubcommandMethod.MakeGenericMethod(subcommand.CommandType); impl.Invoke(this, new object[] { parent, subcommand }); }