请考虑以下最小示例:
Module Module1 Private Enum MyEnum A End Enum Public Sub Main(args As String()) AreEqual(CType(0,MyEnum),MyEnum.A) ' Error here End Sub Private Function AreEqual(Of T)(item1 As T,item2 As T) As Boolean Return False End Function Private Function AreEqual(item1 As Object,item2 As Object) As Boolean Return False End Function End Module
由于某些奇怪的原因,重载分辨率在“Error here”标记的行中失败:
Error 6 Overload resolution Failed because no accessible ‘AreEqual’ is most specific for these arguments:
Private Function AreEqual(item1 As Object,item2 As Object) As Boolean
: Not most specific.
Private Function AreEqual(Of MyEnum)(item1 As MyEnum,item2 As MyEnum) As Boolean
: Not most specific.
为什么第二个功能不是“最具体的”? CType(0,MyEnum)和MyEnum.A都应该是静态键入MyEnum的表达式.
有趣的是,我只能用铸造枚举来重现这个问题. AreEqual(CType(0,Int32),0)和AreEqual(MyEnum.A,MyEnum.A)都编译没有问题.
我知道如何解决这个问题.我知道我可以使用AreEqual(OfEnEnum)(…).这不是问题.我很好奇为什么会发生这种情况.一些编译器的bug?有趣的是,相应的C#代码可以正常工作:
enum MyEnum { A,B } static void Main(string[] args) { AreEqual((MyEnum)0,MyEnum.A); } static bool AreEqual<T>(T item1,T item2) { return false; } static bool AreEqual(object item1,object item2) { return false; }