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
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);
}
10 Replies
ero
ero2mo ago
i assume command.CommandReference returns Type? you need an instance of that type to pass to methodInfo.Invoke. say command.CommandReference returns typeof(Foo). we then get an instance method declared in Foo named Execute. to then Invoke that instance method, we need to specify what instance of Foo to invoke the method on. but since you're passing command.CommandReference to Invoke (which is an instance of Type, not an instance of Foo), it fails.
Spielepapagei
SpielepapageiOP2mo ago
Yes command.CommandReference returns a Type oh so i can't just use the command.CommandReference in the Invoke Method. Thank you :) I got it working I added this line and used now classInstance to Invoke my Method ^^
var classInstance = Activator.CreateInstance(command.CommandReference);
var classInstance = Activator.CreateInstance(command.CommandReference);
ero
ero2mo ago
is there a reason why you can't make this generic?
Spielepapagei
SpielepapageiOP2mo ago
My Commands could be in an different Assembly than i executing in if i'm understanding this correct i can't make it generic then?! Please Correct me if I'm wrong im still Learning.
ero
ero2mo ago
you can simply constrain the generic parameter to some common base type or interface?
Spielepapagei
SpielepapageiOP2mo ago
>.< well i have no plan how that works but i will look into it thank you.
Anton
Anton5w ago
you can, generics are available at runtime
Spielepapagei
SpielepapageiOP5w ago
Idk if that Explains what I'm trying to do but in my lib i want to have all the code required to execute the command parse args and selecting the right command to execute. And in a new Tool "Console app" i only need my lib and the actualy command Logic. + one line to register the command. I'm sorry if that makes no sense that's my first more complex project i'm trying to do. For this you say i Don't need Reflection and just can use generics?
No description
ero
ero5w ago
Some code would be good to look at
Spielepapagei
SpielepapageiOP5w ago
Um I looked around and yeah I found a Library that already exists and is exactly what I need ^^. "Cocona" is it called. But thank you for your answers.

Did you find this page helpful?