使用参数 Func 重载方法

本文关键字:重载 方法 参数 Func | 更新日期: 2023-09-27 17:57:13

我想创建一些接受 Func 参数的重载方法。重载方法应使用参数中定义的最通用类型调用该方法。下面是我的方法的快速示例,以及我想如何调用它们:

public static TResult PerformCaching<TResult, T1>(Func<T1, TResult> func, T1 first, string cacheKey)
{
    return PerformCaching((t, _, _) => func, first, null, null, cacheKey);
}
public static TResult PerformCaching<TResult, T1, T2>(Func<T1, T2, TResult> func, T1 first, T2 second, string cacheKey)
{
    return PerformCaching((t, t2, _) => func, first, second, null, cacheKey);
}
public static TResult PerformCaching<TResult, T1, T2, T3>(Func<T1, T2, T3, TResult> func, T1 first, T2 second, T3 third, string cacheKey)
{
    Model data = Get(cacheKey);
    if(data == null)
    {
        Add(cacheKey);
        data = func.Invoke(first, second, third);
        Update(data);
    }
    return data;
}

有可能让它像这样工作吗?另一个问题是,当func到达最终方法时,它发生了什么。它是使用一个参数执行它(当调用第一个方法时)还是使用所有三个参数调用它。

使用参数 Func<T> 重载方法

不,这种方法行不通。您将尝试将Func<T1, TResult>传递给接受Func<T1, T2, T3, TResult>的方法 - 这根本不起作用。我建议改成这样:

public static TResult PerformCaching<TResult>(Func<TResult> func,
                                              string cacheKey)
{
    // Do real stuff in here
    // You may find ConcurrentDictionary helpful...
}
public static TResult PerformCaching<T1, TResult>
    (Func<T1, TResult> func, T1 first, string cacheKey)
{
    return PerformCaching(() => func(first), cacheKey);
}
public static TResult PerformCaching<T1, T2, TResult>
    (Func<T1, T2, TResult> func, T1 first, T2 second, string cacheKey)
{
    return PerformCaching(() => func(first, second), cacheKey);
}
public static TResult PerformCaching<T1, T2, T3, TResult>
    (Func<T1, T2, T3, TResult> func, T1 first, T2 second, T3 third,
     string cacheKey)
{
    return PerformCaching(() => func(first, second, third), cacheKey);
}

你必须从Func<T, T1, T2>投影到Func<T, T1, T2, T3>。这并不难,但我不确定这是最好的方法。您还有其他一般问题,例如转换为模型(我将其转换为字符串)。更好的方法可能是类似 Cache.Retrieve<TResult>(string cashKey, Func<TResult> missingItemFactory) .然后你会像Cache.Retrieve("model1", () => repository.Get<Model>(myId))一样调用,然后只是在你的方法中调用if (data == null) data = missingItemFactory();

无论如何,解决方案如下。

void Main()
{
    Func<string, string> f1 = s => "One";
    Func<string, string, string> f2 = (s1, s2) => "Two";
    Func<string, string, string, string> f3 = (s1, s2, s3) => "Three";
    Console.WriteLine(PerformCaching(f1, "one", "f1"));
    Console.WriteLine(PerformCaching(f1, "one", "f1"));
    Console.WriteLine(PerformCaching(f2, "one", "two", "f2"));
    Console.WriteLine(PerformCaching(f2, "one", "two", "f2"));
    Console.WriteLine(PerformCaching(f3, "one", "two", "three", "f3"));
    Console.WriteLine(PerformCaching(f3, "one", "two", "three", "f3"));
}
// Define other methods and classes here
public static TResult PerformCaching<TResult, T1>(Func<T1, TResult> func, T1 first, string cacheKey)
{
    return PerformCaching<TResult, T1, string, string>((t, t2, t3) => func(t), first, null, null, cacheKey);
}
public static TResult PerformCaching<TResult, T1, T2>(Func<T1, T2, TResult> func, T1 first, T2 second, string cacheKey)
{
    return PerformCaching<TResult, T1, T2, string>((t, t2, t3) => func(t, t2), first, second, null, cacheKey);
}
public static TResult PerformCaching<TResult, T1, T2, T3>(Func<T1, T2, T3, TResult> func, T1 first, T2 second, T3 third, string cacheKey)
{
    TResult data = Get<TResult>(cacheKey);
    if(data == null)
    {
        Add(cacheKey);
        data = func.Invoke(first, second, third);
        Update(data);
    }
    return data;
}
public static T Get<T>(string CashKey) { return default(T); }
public static void Add(string CashKey) { }
public static void Update<T>(T data) { }