public interface ICommandService
{
void Handle(Command command);
void RegisterHandler<T>(Action<T> handler);
}
public class CampaignCommandHandler
{
.
.
.
public void Handle(RegisterCampaignCommand command)
{
//Some code here
}
.
.
}
目前像这样手动向处理程序注册命令。
commandService.RegisterHandler<RegisterCampaignCommand>(campaignCommandHandler.Handle);
我有很多命令,每个命令在 CampaignCommandHandler 类中都有一个句柄函数。
var handles = campaignCommandHandler.GetType().GetMethods().Where(x => x.Name == "Handle");
foreach (var handle in handles)
{
var command = handle.GetParameters().FirstOrDefault(x => x.ParameterType.BaseType == typeof(Command));
// This is something, I want to do, which is obviously not the right as is
commandService.RegisterHandler<command.GetType()>(GetActionfromMethodInfo(handle));
}
我正在尝试执行上述操作,但不确定如何使用反射来完成。我想知道是否有人可以指导我。
谢谢。