C# 将参数作为方法参数传递的委托-如何访问委托参数?

C# 将参数作为方法参数传递的委托-如何访问委托参数?,c#,asp.net-mvc-4,delegates,C#,Asp.net Mvc 4,Delegates,我试图修改委托(本身作为参数传递)以接受附加参数。这是原始方法签名: public static T GetCacheItem<T>(String key, Func<T> cachePopulate) 我确信,只要将它添加到委托方法签名中就可以了 我想你想要: public static T GetCacheItem<T>(String key, Func<string, T> cachePopulate) { // TODO: Try

我试图修改委托(本身作为参数传递)以接受附加参数。这是原始方法签名:

public static T GetCacheItem<T>(String key, Func<T> cachePopulate)
我确信,只要将它添加到委托方法签名中就可以了

我想你想要:

public static T GetCacheItem<T>(String key, Func<string, T> cachePopulate)
{
    // TODO: Try to get it from the cache

    // But if not...
    T result = cachePopulate(key);
    // TODO: Cache it
    return result;
}
publicstatict GetCacheItem(字符串键,Func cachePopulate)
{
//TODO:尝试从缓存中获取它
//但如果不是。。。
T结果=缓存填充(键);
//TODO:缓存它
返回结果;
}

但基本上,您需要决定向学员输入的内容的含义。

我们应该如何知道?也许你只是想输入
。。。但基本上,我们不能仅仅从签名中分辨出含义。当然你需要决定你想让这个参数是什么意思。如果你添加了这个参数,你大概知道该传递什么了吧?
public static T GetCacheItem<T>(String key, string newparam, Func<string, T> cachePopulate)
{
    ...
    cachePopulate(newparam);
}
public static T GetCacheItem<T>(String key, Func<string, T> cachePopulate)
{
    // TODO: Try to get it from the cache

    // But if not...
    T result = cachePopulate(key);
    // TODO: Cache it
    return result;
}