C# EasyNetQ中的多态请求响应

C# EasyNetQ中的多态请求响应,c#,rabbitmq,easynetq,C#,Rabbitmq,Easynetq,我想知道我是否可以在EasyNetQ中以类似于 这里有一个例子,它使用Mike Hadlow的例子与Cats和Dogs(检查上面的链接),但是Animal是一个类而不是一个接口。我正在尝试做一些类似的事情: Animal d = new Dog(); bus.Request<Animal, string>(d); bus.Request<string>(d.getType(), d); 提前多谢 编辑:更多信息: 目前在我的代码库中,我使用(扩展)方法bus.Publ

我想知道我是否可以在EasyNetQ中以类似于

这里有一个例子,它使用Mike Hadlow的例子与
Cat
s和
Dog
s(检查上面的链接),但是
Animal
是一个类而不是一个接口。我正在尝试做一些类似的事情:

Animal d = new Dog();
bus.Request<Animal, string>(d);
bus.Request<string>(d.getType(), d);
提前多谢

编辑:更多信息:


目前在我的代码库中,我使用(扩展)方法
bus.Publish(msg.GetType(),msg)
以便根据
msg
的实例类型将消息正确路由到正确的侦听器,我只在运行时知道该实例类型。这允许我避免像
bus.Publish(msg)
bus.Publish(msg)这样的代码重复
通过提供相同的函数调用(无代码流分支)作为发布不同类型消息的方式。我也想使用一个非泛型的
bus.Request()
方法,并像
bus.Request(msg.GetType(),string.GetType(),msg)
那样使用它,以避免像
bus.Request(msg)
bus.Request(msg)那样多次调用此方法。希望我可以避免这样做:。

路由是通过TRequest和TreResponse的组合进行的,因此您必须确保请求者和响应者的路由相同。TRequest、TreResponse的任何子类型组合都应正确序列化。

尝试使用dynamic关键字;在发布命令之前

  public void MethodName(IAnimal animal)
    {
        dynamic actualObject = animal;
        bus.Publish(actualObject);
    }
  public void MethodName(IAnimal animal)
    {
        dynamic actualObject = animal;
        bus.Publish(actualObject);
    }