【问题标题】:c# Expression lambda to vb.netc# 表达式 lambda 到 vb.net
【发布时间】:2015-09-10 08:26:50
【问题描述】:

使用转换器(Redgate,Telerik,...)后,我无法将此表达式 c# 转换为 vb.net

if (afterItemRemoved != null)
        {
            cacheItemPolicy.RemovedCallback = x => afterItemRemoved(
                x.CacheItem.Key,
                (T)x.CacheItem.Value);
        }

我尝试了以下表达式但没有成功(Reflector 8.5 de RedGate y converter.telerik.com)

If (afterItemRemoved IsNot Nothing) Then
    cacheItemPolicy.RemovedCallback = x => afterItemRemoved.Invoke(x.CacheItem.Key, DirectCast(x.CacheItem.Value, T))
End If


If afterItemRemoved IsNot Nothing Then
    cacheItemPolicy.RemovedCallback = Function(x) afterItemRemoved(x.CacheItem.Key, DirectCast(x.CacheItem.Value, T))
End If

【问题讨论】:

  • 你尝试过的看起来与原来的完全不同。为什么您尝试的函数名称不同?
  • 是两个类似的方法,是转录错误。感谢您的评论。
  • 将“!=”(C#)转换为“>”(VB.NET)的工具肯定不是太有用。
  • 这很奇怪,你是对的。

标签: c# vb.net lambda expression


【解决方案1】:

查看RemovedCallback 的文档,我们可以看到所需的委托签名是void 方法(VB.Net 中的Sub)(请参阅CacheEntryRemovedCallback)。

所以所需的 lambda 表达式必须是“Sub Lambda”而不是“Function lambda”

If afterItemRemoved IsNot Nothing Then
    cacheItemPolicy.RemoveCallback =
        Sub(x) afterItemRemoved(x.CacheItem.Key, DirectCast(x.CacheItem.Value, T))
End If

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-11-12
    • 1970-01-01
    • 2011-05-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多