Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8774845
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T18:40:14+00:00 2026-06-13T18:40:14+00:00

I have made a little CQRS API for our system I tried to replace

  • 0

I have made a little CQRS API for our system
I tried to replace some reflection code by using the dynamic keyword but it does not work

Each command handler is Generic CommandHandler<TCommand> with a Method void Execute(TCommand Command)

With reflection it works

public void Invoke(Contracts.Commands.Command command)
{
    var handlerType = types[command.GetType()];

    var handler = kernel.Get(handlerType);
    var method = handlerType.GetMethod("Execute");
    method.Invoke(handler, new object[] { command });
}

Kernel.Get is the untyped version of kernel.Get<T> in our IoC (Ninject). This works and the generic method Execute of T fires

This code fails with a argument mismatch exception

public void Invoke(Contracts.Commands.Command command)
{
    var handlerType = types[command.GetType()];

    dynamic handler = kernel.Get(handlerType);
    handler.Execute(command);
}

If i statically declare the type it works with dynamic

dynamic handler = new TestCommandHandler();
handler.Execute(new TestCommand());

edit: Some more info to answer questions in comments

  • handlerType is a concrete class that implements the abstract class CommandHandler<TCommand> where TCommand : Command
  • The execute method is declared in the abstract class as public virtual void Execute(TCommand command)
  • TestCommand implements the abstract Class Command
  • Strange thing is that the same Handler and command works if I statically declare them strongly typed, last example (In the real world example I have dependencies in the constructor though

    edit2

  • Stacktrace

at CallSite.Target(Closure , CallSite , Object , Command ) at
System.Dynamic.UpdateDelegates.UpdateAndExecuteVoid2[T0,T1](CallSite
site, T0 arg0, T1 arg1) at
XXX.Business.Command.CommandHandlerInvoker.Invoke(Command
command) in
C:\XXX.Business\Command\CommandHandlerInvoker.cs:line
29 at XXX.Web.XXXService.Execute(Command command) in
C:\XXX.Web\ExfiService.svc.cs:line 29 at
XXX.Web.Controllers.ComplianceController.XXX(XXXViewModel
viewModel) in
C:\XXX.Web\Controllers\XXXController.cs:line
52 at lambda_method(Closure , ControllerBase , Object[] ) at
System.Web.Mvc.ActionMethodDispatcher.Execute(ControllerBase
controller, Object[] parameters) at
System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext
controllerContext, IDictionary2 parameters) at
System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext
controllerContext, ActionDescriptor actionDescriptor, IDictionary
2
parameters) at
System.Web.Mvc.ControllerActionInvoker.<>c_DisplayClass15.b_12()
at
System.Web.Mvc.ControllerActionInvoker.InvokeActionMethodFilter(IActionFilter
filter, ActionExecutingContext preContext, Func`1 continuation)

  • Exception

The best overloaded method match for
XXX.Business.Command.CommandHandler<XXX.Contracts.Commands.TestCommand>.Execute(XXX.Contracts.Commands.TestCommand)' has some invalid arguments

Error message is stating the abstract class of T not the concrete class, but handlerType in above code is the concrete class

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T18:40:15+00:00Added an answer on June 13, 2026 at 6:40 pm

    You need to change your code to look like this:

    public void Invoke(Contracts.Commands.Command command)
    {
        var handlerType = types[command.GetType()];
    
        dynamic handler = kernel.Get(handlerType);
        dynamic cmd = command;
        handler.Execute(cmd);
    }
    

    Notice that the command parameter is first assigned to a dynamic local variable (cmd). This allows the type of cmd to be evaluated at runtime and keeps the call to Execute truly dynamic; where as NOT doing it this way, the Execute method is assumed to have a fixed signature of Execute(Contracts.Commands.Command command)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Here is some of the main code for a little app I have made
I have made a YUI module a little like the code shown below, which
So... I have some little flash graphs I made that pull data from an
So I have made a little piece of code that will insert and manipulate
i m new to Grails and using some Shiro security. I have made a
So some web apps I've made have little features of ajax. Simple things usually,
I have made little test (with Jsoup 1.6.1): String s = +Jsoup.parse(<td></td>).select(td).size(); System.out.println(Selected elements
I have made this code that makes some visual tiles that fades in and
Visual Studio 2010 Express, Windows Forms. Have made myself my first little application which
I have made a code that read data from flash Nand (without filesystem). fd

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.