标签: explicit-implementation

从其他接口继承的接口的显式 C# 接口实现

考虑以下三个接口:

interface IBaseInterface
{
    event EventHandler SomeEvent;
}

interface IInterface1 : IBaseInterface
{
    ...
}

interface IInterface2 : IBaseInterface
{
    ...
}
Run Code Online (Sandbox Code Playgroud)

现在考虑以下实现 IInterface1 和 IInterface 2 的类:

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

    event EventHandler IInterface2.SomeEvent
    {
        add { ... }
        remove { ... }
    }
}
Run Code Online (Sandbox Code Playgroud)

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

Foo 类如何同时实现 IInterface1 和 IInterface2?

c# implementation explicit interface explicit-implementation

5
推荐指数
2
解决办法
1229
查看次数

如何选择是实现接口还是显式实现接口?

有两种方法可以实现接口:

interface IMyInterface
{
    void Foo();
}

class IImplementAnInterface : IMyInterface
{
    public void Foo()
    {
    }
}
// var foo = new IImplementAnInterface();
// foo.Foo(); //Ok
// ((IMyInterface)foo).Foo(); //Ok

class IExplicitlyImplementAnInterface : IMyInterface
{
    void IMyInterface.Foo()
    {
    }
}
// var foo = new IExplicitlyImplementAnInterface();
// foo.Foo(); //ERROR!
// ((IMyInterface)foo).Foo(); //Ok
Run Code Online (Sandbox Code Playgroud)

不同之处在于,如果接口是显式实现的,那么在允许某人调用该Foo方法之前,它必须实际上被转换为给定的接口.

如何决定使用哪个?

c# interface explicit-implementation

4
推荐指数
1
解决办法
179
查看次数

为什么Array类明确地实现Ilist接口而不是隐式?

我的目标语言是带有.net框架的C#.我想知道这个主题背后的意义或原因是什么?

任何建议和建议都会得到高度赞赏.

编辑

为什么我问这个问题?因为现在,Array类的一些有用的成员就像索引一样在演员背后!我想知道如果微软分裂ilist接口会更好吗?

.net arrays interface explicit-interface explicit-implementation

4
推荐指数
1
解决办法
1987
查看次数

为什么C#不支持显式实现虚拟方法?

可以显式实现C#中的接口方法,以便在将实例显式强制转换为接口类型时调用它们的实现.为什么在类的虚方法上也不支持这个?

尽管解决"多重继承"问题对于接口来说是独一无二的,但似乎由于其他原因明确实现了成员对接口有用,它们对虚拟方法也很有用.我们想到一个更清洁的返回型协方差模型.

编辑:按要求,一个例子:

public class Foo {
    ...
}

public class Bar : Foo {
   ...
}

class Base {
   abstract Foo A ();
}

class Dervied {
  private Bar _b;

  Bar A () {
    return _b;
  }

  Foo Base.A () {
    return _b;
  }
}
Run Code Online (Sandbox Code Playgroud)

我知道使用辅助方法来模拟这个,但是净效果似乎具有显式实现可能具有的任何不良特性,但具有更脏的API.我的问题的关键不在于如何做返回类型协方差,而是为什么虚拟方法不支持类似的接口机制.

c# virtual-functions interface explicit-implementation

3
推荐指数
1
解决办法
1350
查看次数

将显式接口实现与动态对象一起使用

我正在试验接口的显式实现.这是用当前上下文中无效的方法去除intellisense.使用/ practical-applications-of-adaptive-application-of-fluent-builder-context /作为参考.为了证明它们不可调用,我认为我可以使用dynamic关键字,因为至少我的代码会编译.它确实编译,但它没有按预期工作.动态变量可以访问类方法,但不能访问显式实现的接口方法.

public interface IAmInterface
{
    void Explicit();
    void Implicit();
}

public class Implementation : IAmInterface
{
    void IAmInterface.Explicit()
    {
    }

    public void Implicit()
    {
    }
    public static Implementation BeginBuild()
    {
        return new Implementation();
    }
}
Run Code Online (Sandbox Code Playgroud)

以下是3个测试来证明我的观点

[Test]
public void TestWorksAsExpected() //Pass
{
    var o = Implementation.BeginBuild();
    o.Implicit();
}

[Test]
public void TestDoesNotWorkWithExplicitImplementation() //Fails
{
    dynamic o = Implementation.BeginBuild();
    o.Explicit();
}

[Test]
public void ButWorksForImplicitImplementation() //Pass
{
    dynamic o = Implementation.BeginBuild();
    o.Implicit();
}
Run Code Online (Sandbox Code Playgroud)

有人会善意解释这个的原因吗?我想要这个功能的一个例子是证明我不能在TennisGame中添加两个以上的玩家.

dynamic o = …
Run Code Online (Sandbox Code Playgroud)

c# dynamic explicit-implementation

3
推荐指数
1
解决办法
1293
查看次数

C++ - 允许通过基类(接口)访问,禁止通过派生类访问(具体实现)?

假设我有纯抽象类IHandler和我的类派生于它:

class IHandler
{
public:
   virtual int process_input(char input) = 0;
};

class MyEngine : protected IHandler
{
public:
   virtual int process_input(char input) { /* implementation */ } 
};
Run Code Online (Sandbox Code Playgroud)

我希望在我的MyEngine中继承该类,以便我可以传递MyEngine*给任何期望的人,IHandler*并让他们能够使用process_input.但是我不想允许访问,MyEngine*因为我不想公开实现细节.

MyEngine* ptr = new MyEngine();
ptr->process_input('a');                           //NOT POSSIBLE
static_cast<IHandler*>(ptr)->process_input('a');   //OK
IHandler* ptr2 = ptr;                              //OK
ptr2->process_input('a');                          //OK
Run Code Online (Sandbox Code Playgroud)

这可以通过受保护的继承和隐式转换来完成吗?我只是设法得到:

从"MyEngine*"到"IHandler*"的转换存在,但无法访问

由于我来自C#后台,这基本上是C#中的显式接口实现.这是C++中的有效方法吗?

额外:

为了更好地了解我为什么要这样做,请考虑以下事项:

TcpConnection通过TCP实现通信,并且在其构造函数中需要指向接口的指针ITcpEventHandler.当TcpConnection在套接字上获取某些数据时,它会将该数据传递给它ITcpEventHandler使用ITcpEventHandler::incomingData,或者当它轮询它使用的传出数据时ITcpEventHandler::getOutgoingData.

我的类HttpClient使用TcpConnection(聚合)并将自身传递给 …

c++ inheritance abstract-class casting explicit-implementation

2
推荐指数
1
解决办法
2127
查看次数