5

次の3つのインターフェイスについて考えてみます。

interface IBaseInterface
{
    event EventHandler SomeEvent;
}

interface IInterface1 : IBaseInterface
{
    ...
}

interface IInterface2 : IBaseInterface
{
    ...
}

ここで、IInterface1とIInterface2の両方を実装する次のクラスについて考えてみます。

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

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

SomeEventはIInterface1またはIInterface2の一部ではなく、IBaseInterfaceの一部であるため、これによりエラーが発生します。

クラスFooはIInterface1とIInterface2の両方をどのように実装できますか?

4

3 に答える 3

5

SomeEventは、IInterface1またはIInterface2の一部ではなく、IBaseInterfaceの一部です。

class Foo : IInterface1, IInterface2
{
    event EventHandler IBaseInterface.SomeEvent {
        add { ... }
        remove { ... }
    }
}
于 2010-05-09T00:49:26.917 に答える
5

ジェネリックを使用できます:

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 { ... }
    }
}    
于 2011-01-07T02:28:28.467 に答える
2
interface IBaseInterface
{
    event EventHandler SomeEvent;
}

interface IInterface1 : IBaseInterface
{
    event EventHandler SomeEvent;
}

interface IInterface2 : IBaseInterface
{
    event EventHandler SomeEvent;
}

class Foo : IInterface1, IInterface2
{

    public event EventHandler SomeEvent
    {
        add { }
        remove { }
    }

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


    event EventHandler IInterface2.SomeEvent
    {
        add { }
        remove { }
    }
}  
于 2010-05-09T01:07:33.900 に答える