多个泛型歧义 [英] Multiple Generics ambiguity

查看:197
本文介绍了多个泛型歧义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

的codeS下面是完全一样的,所不同的是一个是C#和另一种是VB.Net。 C#编译就好了,但VB.Net抛出警告:

  

接口System.IObserver(中富)'不明确与另一   实现的接口System.IObserver(条码)由于IN和   在接口IObserver(在T)''出'参数

为什么VB.Net显示警告信息,而不是C#?而最重要的,我怎么能解决这个问题呢?

观测:我在使用.NET Framework 4和Visual Studio 2010旗舰版

VB.Net code:

 模块模块1

    副主()

    结束小组

    公共类Foo
    末级
    公共类酒吧
    末级
    公共类旁观者
        实现IObserver(中富)
        实现IObserver(条码)

#REGION意味着
        公用Sub OnCompleted()实现System.IObserver(条码).OnCompleted

        结束小组

        公用Sub的OnError([错误]作为System.Exception的)实现System.IObserver(条码).OnError

        结束小组

        公用Sub OnNext(价值吧)实现System.IObserver(条码).OnNext

        结束小组

        公用Sub OnCompleted1()实现System.IObserver(中富).OnCompleted

        结束小组

        公用Sub OnError1([错误]作为System.Exception的)实现System.IObserver(中富).OnError

        结束小组

        公用Sub OnNext1(价值为Foo)实现System.IObserver(中富).OnNext

        结束小组
#END地区

    末级

前端模块
 

C#code:

 类节目{
        静态无效的主要(字串[] args){
        }
    }

    公共类Foo {}
    公共类酒吧{}
    公共类旁观者:IObserver<富>中IObserver<酒吧GT; {
        #地区IObserver<富>会员

        公共无效OnCompleted(){
            抛出新的NotImplementedException();
        }

        公共无效的OnError(异常错误){
            抛出新的NotImplementedException();
        }

        公共无效OnNext(美孚值){
            抛出新的NotImplementedException();
        }

        #endregion

        #地区IObserver<酒吧GT;会员


        公共无效OnNext(酒吧值){
            抛出新的NotImplementedException();
        }

        #endregion
    }
 

解决方案

总结:

  • VB似乎不必要在这里提出警告。我会提到它的VB的测试人员时,他们从圣诞假期回来。
  • 这是一个可疑的编程习惯,无论是安全与否;这是一个有点陌生实现两个版本的相同的接口。
  • 如果不是你选择了一个的接口,如的IEnumerable< T> 则警告将是合理的。如果你有一个对象,既序列乌龟和长颈鹿的顺序,那么当你暗中将其转换为动物的顺序会发生什么?你得到海龟或长颈鹿?运行时只挑选一个,这并不一定是你想要的行为。

对于最后一点有一些有趣的讨论,请参阅该意见我关于这个问题的2007年文章:

<一个href="http://blogs.msdn.com/b/ericlippert/archive/2007/11/09/covariance-and-contravariance-in-c-part-ten-dealing-with-ambiguity.aspx" rel="nofollow">http://blogs.msdn.com/b/ericlippert/archive/2007/11/09/covariance-and-contravariance-in-c-part-ten-dealing-with-ambiguity.aspx

The codes below are exactly the same, except that one is C# and the other one is VB.Net. C# compiles just fine, but VB.Net throws the warning:

Interface 'System.IObserver(Of Foo)' is ambiguous with another implemented interface 'System.IObserver(Of Bar)' due to the 'In' and 'Out' parameters in 'Interface IObserver(Of In T)'

Why does VB.Net show the warning and not C#? And most important, how can I resolve this problem?

Obs: I'm using .Net Framework 4 with Visual Studio 2010 Ultimate.

VB.Net Code:

Module Module1

    Sub Main()

    End Sub

    Public Class Foo
    End Class
    Public Class Bar
    End Class
    Public Class Beholder
        Implements IObserver(Of Foo)
        Implements IObserver(Of Bar)

#Region "Impl"
        Public Sub OnCompleted() Implements System.IObserver(Of Bar).OnCompleted

        End Sub

        Public Sub OnError([error] As System.Exception) Implements System.IObserver(Of Bar).OnError

        End Sub

        Public Sub OnNext(value As Bar) Implements System.IObserver(Of Bar).OnNext

        End Sub

        Public Sub OnCompleted1() Implements System.IObserver(Of Foo).OnCompleted

        End Sub

        Public Sub OnError1([error] As System.Exception) Implements System.IObserver(Of Foo).OnError

        End Sub

        Public Sub OnNext1(value As Foo) Implements System.IObserver(Of Foo).OnNext

        End Sub
#End Region

    End Class

End Module

C# Code:

 class Program {
        static void Main(string[] args) {
        }
    }

    public class Foo { }
    public class Bar { }
    public class Beholder : IObserver<Foo>, IObserver<Bar> {
        #region IObserver<Foo> Members

        public void OnCompleted() {
            throw new NotImplementedException();
        }

        public void OnError(Exception error) {
            throw new NotImplementedException();
        }

        public void OnNext(Foo value) {
            throw new NotImplementedException();
        }

        #endregion

        #region IObserver<Bar> Members


        public void OnNext(Bar value) {
            throw new NotImplementedException();
        }

        #endregion
    }

解决方案

Summing up:

  • VB appears to be giving the warning unnecessarily here. I'll mention it to the VB testers when they're back from Christmas vacation.
  • This is a suspicious programming practice regardless of whether it is safe or not; it's a bit strange to implement two versions of the same interface.
  • If instead you chose a covariant interface like IEnumerable<T> then the warning would be justified. If you have an object that is both a sequence of Turtles and a sequence of Giraffes, then what happens when you implicitly convert it to sequence of Animal? Do you get Turtles or Giraffes? The runtime just picks one, which is not necessarily the behaviour you want.

For some interesting discussion of the last point see the comments to my 2007 article on the subject:

http://blogs.msdn.com/b/ericlippert/archive/2007/11/09/covariance-and-contravariance-in-c-part-ten-dealing-with-ambiguity.aspx

这篇关于多个泛型歧义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆