如何结束一个函数及其参数...某种“超级委托”?(How to wrap up a function and its parameters… sort of a “super delegate”?)

编程入门 行业动态 更新时间:2024-10-25 02:26:46
如何结束一个函数及其参数...某种“超级委托”?(How to wrap up a function and its parameters… sort of a “super delegate”?)

我有一大堆代码在一个中等复杂的重试和try / catch逻辑中执行“文件放置”操作。 这工作正常,看起来大致如下:

while (...) { try { FilePut(source, destination) } catch () { //Check exception type, possibly re-throw, possibly return, possibly increment //counters being checked by while loop } }

这个逻辑的细节不是问题。 但是我意识到我还有其他一些操作也需要在同一种逻辑中执行,我想避免复制并在我的应用程序中粘贴这个逻辑。 我想将它移动到一个函数并重用该函数。 该函数必须对要调用的操作进行某种引用,try逻辑将执行该操作(文件put,文件get,等等)。

对于委托来说这似乎是个好地方,但问题是,每个操作都有不同的签名,所以我不知道如何能够编写我的上述逻辑以便能够调用“任何”操作。

有没有一种很好的方法在C#中做到这一点?

I have a chunk of code that does a "file put" operation inside a moderately complicated piece of retry and try/catch logic. This is working fine, it looks roughly like this:

while (...) { try { FilePut(source, destination) } catch () { //Check exception type, possibly re-throw, possibly return, possibly increment //counters being checked by while loop } }

The details of this logic aren't the issue. But I'm realizing I have several other operations that also need to execute inside this same kind of logic and I want to avoid copy and pasting this logic around my application. I'd like to move it to a function and reuse that function. That function would have to take some sort of reference to the operation to be called, and the try logic would execute that operation (file put, file get, whatever).

This seems like a great place for a delegate, but the problem is, each of these operations has a different signature, so I'm not sure how to be able to write my above logic to be able to call "any" operation.

Is there a good way to do this in C#?

最满意答案

您需要一个Action委托来隐藏所有不同的签名。 像这样的东西:

public void DoAction(Action action) { // Make the boilerplate code whatever you need it to be. // I've just used a try catch for simplicity. try { // Call the action at the appropriate time. action(); } catch { // Handle any exceptions as you wish. } }

然后,为了能够处理具有不同签名的操作,您可以定义一些重载,这些重载采用不同类型的通用Action<T>委托和所有必需的参数。 这些重载将“curry”泛型动作及其对简单Action参数:

public void DoAction<T>(Action<T> action, T arg1) { DoAction(() => action(arg1)); } public void DoAction<T1, T2>(Action<T1, T2> action, T1 arg1, T2 arg2) { DoAction(() => action(arg1, arg2)); } public void DoAction<T1, T2, T3>(Action<T1, T2, T3> action, T1 arg1, T2 arg2, T3 arg3) { DoAction(() => action(arg1, arg2, arg3)); } // etc...

使用:

public void SomeOtherMethod() { DoAction(MethodThatTakesTwoInts, 42, 23); DoAction(MethodThatTakesAString, "Don't Panic!"); }

此外,如果您不熟悉它们,请查看Func代表的相关系列以及良好的衡量标准。

You need an Action delegate to hide all of the different signatures. Something like this:

public void DoAction(Action action) { // Make the boilerplate code whatever you need it to be. // I've just used a try catch for simplicity. try { // Call the action at the appropriate time. action(); } catch { // Handle any exceptions as you wish. } }

Then, to be able to handle actions with different signatures, you can define a few overloads that take different types of the generic Action<T> delegates and all of the necessary arguments. These overloads will "curry" the generic action and its arguments to a plain Action:

public void DoAction<T>(Action<T> action, T arg1) { DoAction(() => action(arg1)); } public void DoAction<T1, T2>(Action<T1, T2> action, T1 arg1, T2 arg2) { DoAction(() => action(arg1, arg2)); } public void DoAction<T1, T2, T3>(Action<T1, T2, T3> action, T1 arg1, T2 arg2, T3 arg3) { DoAction(() => action(arg1, arg2, arg3)); } // etc...

To use:

public void SomeOtherMethod() { DoAction(MethodThatTakesTwoInts, 42, 23); DoAction(MethodThatTakesAString, "Don't Panic!"); }

Also, if you not familiar with them, take a look at the related family of Func delegates as well for good measure.

更多推荐

本文发布于:2023-07-26 02:15:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1269592.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:参数   一个函数   结束   wrap   function

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!