有代码:
Private Sub InsertItemInCache(Of T)(ByVal item As CachedItem(Of T), ByVal dependency As AggregateCacheDependency, _
ByVal key As String, ByVal updateCallBack As CacheItemUpdateCallback)
CacheItemUpdateCallback签名是:
Sub CacheItemUpdateCallback(ByVal key As String, ByVal reason As CacheItemUpdateReason, _
ByRef expensiveObject As Object, ByRef dependency As CacheDependency, ByRef absoluteExpiration As Date, _
ByRef slidingExpiration As TimeSpan)
我想使用lamba表达式调用InsertItemInCache函数。 此代码未编译:
InsertItemInCache(cachedItem, dependency, key, Function(k, r, e, d, a, s) CacheItemUpdateCallback(k, r, e, d, a, s))
它表示表达式不会产生值
如果我将 Sub CacheItemUpdateCallback 更改为功能CacheItemUpdateCallback 它也没有编译并说嵌套函数没有与委托'委托Sub CacheItemUpdateCallback(键作为字符串,作为System.Web.Caching.CacheItemUpdateReason,ByRef expensiveObject作为对象,ByRef依赖作为System.Web)相同的签名.Caching.CacheDependency,ByRef absoluteExpiration As Date,ByRef slidingExpiration As System.TimeSpan)'
任何人都可以帮我通过lambda表达式调用此方法吗?我希望将来使用闭包并以这种方式调用此函数:
InsertItemInCache(cachedItem, dependency, key, Function(k, r, e, d, a, s) CacheItemUpdateCallbackNew(k, r, e, d, a, s, additionalParameter1, additionalParameter2, additionalParameter3))
答案 0 :(得分:1)
我猜这是一个较老的vb.net,因为VS2010发布的vb10可以处理子Lambda,但你还有另一个问题。
byref
。 Func(Of Tin, Tout) using a lambda expression with ByRef argument gives incompatible signature error
不关心未来的关闭......这可能只是
InsertItemInCache(cachedItem, dependency, key, AddressOf CacheItemUpdateCallback)
所以主要问题是lambda表达式无法直接捕获ref [ByRef in VB]或out参数来自封闭方法。