[英]Delegate with parameter passed as a method parameter - how to access the delegate arg?
我正在尝试修改委托(本身作为参数传递)以接受其他参数。 这是原始方法签名:
public static T GetCacheItem<T>(String key, Func<T> cachePopulate)
我将其修改为如下所示:
public static T GetCacheItem<T>(String key, Func<string, T> cachePopulate)
如何使用新的字符串参数? 在方法代码中,我可以看到cachePopulate()
现在期望string arg
,但这是什么? 我实际将什么传递给cachePopulate()
作为参数?
更新:
为了简洁起见,我可能没有很好地解释自己。 也许我应该问过如何调用该方法的更新版本。 无论如何,这些注释使我了解到,附加委托参数实际上是作为GetCacheItem
方法的附加参数传递的,如下所示:
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;
}
但基本上, 您需要确定委托输入的含义。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.