使用冗长的泛型类型的数组减少方法调用的冗长性

时间:2019-05-06 18:30:48

标签: c# .net generics

是否有任何方法可以缩短对此方法的调用?似乎很长,第二个参数是否需要强类型输入?如果已知列表,是否在FindPercent上需要?

Model.FindPercent<IStatisticDisplay>(t => t.FaceoffsWon, 
    new Func<IStatisticDisplay, double?>[] { q => q.FaceoffsWon, q => q.FaceoffsLost }, "-")

方法

public static string FindPercent<T>(this List<T> list, Func<T, double?> numerator, 
    Func<T, double?>[] totals, string defValue)
{
    return Helper.FindPercent(list, numerator, totals, defValue);
}

1 个答案:

答案 0 :(得分:1)

只需使用params并删除类型!

public static string FindPercent<T>(this List<T> list, string defValue, Func<T, double?> numerator, params Func<T, double?>[] totals)
{
   return Helper.FindPercent(list, numerator, totals, defValue);
}

Model.FindPercent("-", t => t.FaceoffsWon, q => q.FaceoffsWon, q => q.FaceoffsLost );