我收到编译器警告“无法推断出常见类型;假设'对象'。”来自以下代码:
Dim occurrence As CacheableDocumentOccurrence = _
If(DirectCast(IdentityMap.GetItem(id), CacheableDocumentOccurrence),
Function() As CacheableDocumentOccurrence
Dim x = New CacheableDocumentOccurrence()
IdentityMap.Add(x)
Return x
End Function)
为什么?
答案 0 :(得分:2)
如果If
不返回CacheableDocumentOccurrence
,则GetItem(id)
语句会返回Nothing
,但会返回Function
(lambda表达式)!
在这两种情况下,您都必须返回CacheableDocumentOccurrence
。
Dim occurrence As CacheableDocumentOccurrence = DirectCast(IdentityMap.GetItem(id)
If occurrence Is Nothing Then
occurrence = New CacheableDocumentOccurrence()
IdentityMap.Add(x)
End If
请注意,您的函数不仅仅是某些代码的括号。它无论如何都不会被执行;相反,结果将是AddressOf函数。