[英]How to pass code blocks (not full methods) as arguments in C#?
我正在csharp(.net 4.0)中构建一个消息传递应用程序,我的类具有发送/接收消息的基本方法:
void sendMessage( string msgBody, string properties);
object getNextMessage();
object getMessageById( string msgId);
这些方法中的每一种都取决于底层连接; 如果连接是陈旧的,我使用try / catch和一些重试逻辑来进行额外的尝试,如下所示:
public object getNextMessage(){
object nextMessage = null;
int retryAttempts = 0;
int MAX_ATTEMPTS = 3;
while( retryAttempts < MAX_ATTEMPTS){
retryAttempts++;
try{
nextMessage = connection.getMessage("queueName");
}catch(Exception e){
}
}
return nextMessage;
}
由于重试逻辑是通用的,我想避免在每个方法中重复相同的代码。 我想创建一个通用的重试功能,并执行以下操作:
public object makeAttempt( CodeBlock codeBlock){
while( retryAttempts < MAX_ATTEMPTS){
retryAttempts++;
try{
return codeBlock.invoke()
}catch(Exception e){
}
}
return null;
}
..我想像这样使用makeAttempt
,或类似的东西:
public object getNextMessage(){
makeAttempt() => {
return connection.getMessage("queueName");
}
}
我回顾了这一点 ,但它涉及将整个函数作为参数传递,我没有这样做。 我还回顾了.net Lambda表达式 ,但我没有看到连接。
我没有做太多C#所以请原谅n00b问题:-)
你最后几乎就在那里 - 你只需要将lambda表达式括在()
因为它是一个方法参数。 您还需要使用makeAttempt
的返回值为getNextMessage
方法提供返回值。 所以:
public object getNextMessage(){
return makeAttempt(() => {
return connection.getMessage("queueName");
});
}
或者更简单地说,使用表达式lambda:
public object getNextMessage(){
return makeAttempt(() => connection.getMessage("queueName"));
}
这是假设CodeBlock
是委托类型,当然,例如
public delegate object CodeBlock();
您还需要更改makeAttempt
以调用Invoke
而不是invoke
- C#区分大小写。 我强烈建议您遵循.NET命名约定,其中方法是PascalCased
而不是camelCased
。
编辑:如评论中所述,您可以使这个通用:
public T CallWithRetries<T>(Func<T> function)
{
for (int attempt = 1; attempt <= MaxAttempts; attempt++)
{
try
{
return function();
}
catch(Exception e)
{
// TODO: Logging
}
}
// TODO: Consider throwing AggregateException here
return default(T);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.