2 回答

TA貢獻(xiàn)1801條經(jīng)驗(yàn) 獲得超16個(gè)贊
我猜你真的想要這個(gè)......
public TResponse ExecuteAndLog<TResponse>(Guid id, string Name, Func<TResponse> method) where TResponse : class
{
try
{
Log(id, Name);
TResponse x = method();
Log(id, Name);
}
catch (Exception ex)
{
Log(id, Name);
throw;
}
}
你會(huì)用
var response = ExecuteAndLog(someGuid, someName, () => _service.Count(fileDate, cycle));
這樣你只需要一個(gè) ExecuteAndLog 的原型。如果您將輸入包含在Func(正如您在示例中所做的那樣),則必須傳遞參數(shù),并且對(duì)于每個(gè)可能的服務(wù)調(diào)用簽名,您都需要不同版本的 ExecuteAndLog。

TA貢獻(xiàn)1848條經(jīng)驗(yàn) 獲得超10個(gè)贊
如果method應(yīng)該接收兩個(gè)參數(shù),則需要傳遞它們:
public TResponse ExecuteAndLog<T1, T2,TResponse>(Guid id, string Name, Func<T1, T2, TResponse> method, T1 arg1, T2 arg2) where TResponse : class
{
try
{
Log(id, Name);
TResponse x = method(arg1, arg2);
Log(id, Name);
return x;
}
catch (Exception ex)
{
Log(id, Name);
throw;
}
}
- 2 回答
- 0 關(guān)注
- 295 瀏覽
添加回答
舉報(bào)