我必须错在这里做一些奇怪的行为(因为真的,什么是我绊倒在另一bug in the Vb.net compiler机会?)与扩展方法泛型约束
我在.NET 2.0 Vb的静态泛型函数代码,我认为这是一次“升级”它是一个扩展方法,但是编译器与
扩展方法“AddIfUnqiue”有 型约束条件永远无法满足 抱怨。
这是一个简单的例子,显示相同的问题。旧的静态版本(其中正常工作),其次是扩展方法
Public Class MyStaticClass
Public Shared Sub AddIfUnqiue(Of T, L As {List(Of T)})(ByVal this As L, ByVal item As T)
If this.IndexOf(item) < 0 Then
this.Add(item)
End If
End Sub
End Class
Module UtilityExtensions
<Extension()> _
Sub AddIfUnqiue(Of T, L As {List(Of T)})(ByVal this As L, ByVal item As T)
'ERROR: Extension method 'AddIfUnqiue' has type constraints that can never be satisfied'
If this.IndexOf(item) < 0 Then
this.Add(item)
End If
End Sub
End Module
在C#的等效代码有没有问题,它只是一个VB问题。
public static void AddIfUnique<T, L>(this L myList, T item) where L : List<T>
{
if (myList.IndexOf(item) < 0)
myList.Add(item);
}
有没有人有任何想法,为什么这不起作用? 这很可能是我的VB约束(我更喜欢C#),但我看不出我做错了什么。
感谢,
你确实是闪亮盔甲的元骑士。非常感谢 :) – 2009-09-03 08:27:04