[英]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.