【发布时间】:2021-01-05 15:54:36
【问题描述】:
是否可以将带有out 参数的方法分配给Func<T1, T2> 方法参数?
我可以用作参数的示例方法:
public string HelloMethod(out bool success)
{
success = true;
return "Hello";
};
我想这样做的原因是我调用了一个库,其中许多(但不是全部)方法具有带有错误代码的 out 参数,并且我想创建一个方法来使用 try catch log 等包装对库的调用:
private TResult MakeLibraryCall(...func...) // what signature?
{
try
{
return func(...); // unclear how to make call with out parameter
}
catch(Exception error)
{
Log(error);
}
return default;
}
此外,我不确定如何调用它。 我希望是这样的:
var greeting = MakeLibraryCall((out success) => HelloMetod(out success));
Console.WriteLine(greeting) // => Hello
Console.WriteLine(success) // => true
任何帮助表示赞赏!
【问题讨论】:
-
这能回答你的问题吗? Func<T> with out parameter
-
@AhmedYousif:不确定如何在我的代码中实现该委托模式?它可能有效,但我不知道如何编写解决方案。
-
你可以检查我的答案足以实现你想做的事