接口明确的C#接口实现从其他接口继承 [英] Explicit C# interface implementation of interfaces that inherit from other interfaces

查看:399
本文介绍了接口明确的C#接口实现从其他接口继承的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请考虑以下三个界面:

interface IBaseInterface
{
    event EventHandler SomeEvent;
}

interface IInterface1 : IBaseInterface
{
    ...
}

interface IInterface2 : IBaseInterface
{
    ...
}

现在考虑下面的类, IInterface1和IInterface 2:

Now consider the following class that implements both IInterface1 and IInterface 2:

class Foo : IInterface1, IInterface2
{
    event EventHandler IInterface1.SomeEvent
    {
        add { ... }
        remove { ... }
    }

    event EventHandler IInterface2.SomeEvent
    {
        add { ... }
        remove { ... }
    }
}


b $ b

这会导致错误,因为SomeEvent不是IInterface1或IInterface2的一部分,它是IBaseInterface的一部分。

This results in an error because SomeEvent is not part of IInterface1 or IInterface2, it is part of IBaseInterface.

类Foo如何实现IInterface1和IInterface2?

How can the class Foo implement both IInterface1 and IInterface2?

推荐答案

您可以使用泛型:

interface IBaseInterface<T> where T : IBaseInterface<T>
{
    event EventHandler SomeEvent;
}

interface IInterface1 : IBaseInterface<IInterface1>
{
    ...
}

interface IInterface2 : IBaseInterface<IInterface2>
{
    ...
}

class Foo : IInterface1, IInterface2
{
    event EventHandler IBaseInterface<IInterface1>.SomeEvent
    {
        add { ... }
        remove { ... }
    }

    event EventHandler IBaseInterface<IInterface2>.SomeEvent
    {
        add { ... }
        remove { ... }
    }
}    

这篇关于接口明确的C#接口实现从其他接口继承的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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