English 中文(简体)
明示 C# 从其他接口继承的接口的接口实施
原标题:Explicit C# interface implementation of interfaces that inherit from other interfaces

3. 审议以下三个接口:

interface IBaseInterface
{
    event EventHandler SomeEvent;
}

interface IInterface1 : IBaseInterface
{
    ...
}

interface IInterface2 : IBaseInterface
{
    ...
}

现在审议实施IInterface1和IInterface 2的以下类别:

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

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

造成这一错误的原因是,一些Event不是IInterface1或IInterface2的一部分,它属于《国际基本原则》的接口。

Foo级如何执行IInterface1和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 { ... }
    }
}    
问题回答

一些Event isn t part of IInterface1 or IInterface2, its a part of IBaseInterface.

class Foo : IInterface1, IInterface2
{
    event EventHandler IBaseInterface.SomeEvent {
        add { ... }
        remove { ... }
    }
}
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 { }
    }
}  




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签