dan*_*die 0 .net c# generics overloading
我有一个CheckDuration带有以下函数签名的重载实用程序方法.
private static Action<int> CheckDuration(Action action)
private static Action<int> CheckDuration<T>(Action<T> action, T arg)
Run Code Online (Sandbox Code Playgroud)
基本上CheckDuration在控制台上打印运行方法所需的时间.
现在,我想检查一个带2个参数的方法的持续时间.
所以我必须创建另一个重载CheckDuration以下方法签名.
private static Action<int> CheckDuration<T, U>(
Action<T, U> action, T arg1, U arg2)
Run Code Online (Sandbox Code Playgroud)
有没有办法更优雅地处理这个问题?
我在考虑类似的事情
private static Action<int> CheckDuration<params T>(
Action<params T> action, params T arg)
Run Code Online (Sandbox Code Playgroud)
,这显然不起作用.
[更新]我现在暂时打开这个问题,看看有没有人为这类问题想出办法.
不幸的是,你已经拥有的是干净利落的.C#不支持使用params泛型类型参数,并且支持类似的东西将非常困难.您的原始解决方案很好,并且在BCL中具有类似代表的精神,就像代理Action具有重载定义以获取不同数量的参数一样.