© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
C#C
C#•11mo ago•
10 replies
Spielepapagei

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
DefaultCommand
.
Can someone please help me I'm sure i made some stupid mistake :/

For Context the
command.CommandReference
command.CommandReference
can't be null it's checked before this Method is called.
The
CommandReference
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
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);
    }
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);
    }
C# banner
C#Join
We are a programming server aimed at coders discussing everything related to C# (CSharp) and .NET.
61,871Members
Resources

Similar Threads

Was this page helpful?
Recent Announcements

Similar Threads

❔ System.Reflection
C#CC# / help
4y ago
BindingFlags of System.Reflection
C#CC# / help
5mo ago
✅ .NET MAUI System.Reflection.TargetInvocationException
C#CC# / help
3y ago
✅ Unable to cast object of type 'System.String' to type 'System.Int32'.
C#CC# / help
2y ago