如何通过Func将要执行的方法发送到C#中的另一个方法?

Zer*_*ama 4 c# rabbitmq

我有一个服务类,看起来像:

class BillingService
{
    public void CheckBillingStatus(BillingOperationRequestDto dto)
    {
    }

    public void AnotherOperationOnBillings(BillingOperationRequestDto dto)
    {
    }
}
Run Code Online (Sandbox Code Playgroud)

我还有另一个类,它从RabbitMq监听一些队列。我想写一些类似的东西:

class MessageListener<T> where T : BaseDto {
        public void GetMessage<T>(Func ... )

        MessageListener<T>(string queueToListen)
        {
        }
}
Run Code Online (Sandbox Code Playgroud)

该代码背后的想法是,我想将其用作:

BillingService bs = new BillingService();
var listener = new MessageListener<BillingOperationRequestDto>();

listener.GetMessage<BillingOperationRequestDto>(bs.CheckBillingStatus);
Run Code Online (Sandbox Code Playgroud)

我不仅要指定队列中期望的数据,还要指定对该数据调用哪种方法。这是正确的方法吗?我考虑过只从队列中获取一条消息,然后再将数据发送给另一类,但没有找到执行该方法的方法,因此决定循环运行GetMessage并指定出现消息时应执行的操作。

更新#1.1:是否可以将委托发送到

listener.GetMessage<BillingOperationRequestDto>(bs.CheckBillingStatus);
Run Code Online (Sandbox Code Playgroud)

如果我在BillingService类中的方法将具有不同的方法签名?例如,

public BillingStatusResult CheckBillingStatus(BillingOperationRequestDto dto)
{
}
public AnotherReturnValue AnotherOperationOnBilling(BillingOperationRequestDto dto, string requestedIp, TimeSpan period)
{
}
Run Code Online (Sandbox Code Playgroud)

Ren*_*nov 5

如@juharr所述,您可以使用泛型委托(类型为,Action<T>或者Func<T, TResult>是否需要从委托中检索结果)。

您可以在问题或文档中找到更多信息

class MessageListener<T> where T : BaseDto {
    public void GetMessage<T>(Action<T> action)
    {
    }

    MessageListener<T>(string queueToListen)
    {
    }
}
Run Code Online (Sandbox Code Playgroud)