Dynamically call a method on a generic target

后端 未结 2 840
旧时难觅i
旧时难觅i 2020-12-19 14:44

I have a generic interface ICommandHandler<> that will have a number of implementations each for processing a specific implementation of ICommand

2条回答
  •  清酒与你
    2020-12-19 15:22

    Try this

    dynamic handler=Activator.CreateInstance(handlerType);
    try
      {
             handler.Handle((dynamic)command);
       }
    
       catch
       {
       // do whatever you want 
       }
    

提交回复
热议问题