Reflection error Object type T does not match target type System.RuntimeType.

I'm trying to get the "Execute" method from in this case DefaultCommand.
Can someone please help me I'm sure i made some stupid mistake :/

For Context the command.CommandReference can't be null it's checked before this Method is called.
The CommandReference is the Class i'm trying to call a method from.

System.Reflection.TargetException: Object type Papageis.Cli.Commands.DefaultCommand does not match target type System.RuntimeType.
  at void System.Reflection.MethodInvokerCommon.ValidateInvokeTarget(object target, MethodBase method)
  at object System.Reflection.RuntimeMethodInfo.Invoke(object obj, BindingFlags invokeAttr, Binder binder, object[] parameters, CultureInfo 
     culture)
  at object System.Reflection.MethodBase.Invoke(object obj, object[] parameters)
  at Task<int> Papageis.Cli.Services.CliExecutionService.ExecuteCommand(CommandContext context, ConfiguredCommand command) in
     C:\Users\Ole\Documents\GitHub\Spielepapagei\Papageis.Cli\Papageis.Cli\Services\CliExecutionService.cs:87


private Task<int> ExecuteCommand(CommandContext context, ConfiguredCommand command)
    {
        try
        {
            var methodInfo = command.CommandReference!.GetMethod(nameof(Command.Execute), BindingFlags.Public | BindingFlags.Instance);
            if (methodInfo == null)
                throw new Exception();
            methodInfo.Invoke(command.CommandReference, [context]);
        }
        catch (Exception ex)
        {
            AnsiConsole.WriteException(ex);
            return Task.FromResult(-1);
        }

        return Task.FromResult(0);
    }
Was this page helpful?