如果方法不可见,WCF代理如何实现ICommunicationObject?

Ale*_*sko 6 wcf interface channelfactory implements

例如,WCF通道(通过ChannelFactory创建)如何实现ICommunicationObject,但不公开Close()方法,除非您将代理转换为ICommunicationObject?那有意义吗?

我在今天回家的路上想到了这一点,无法在脑海中弄明白.也许我问的是错误的问题?也许我问一个愚蠢的问题?:)

这是一种忍者伎俩吗?

Ree*_*sey 5

这是通过显式接口实现完成的.

假设你有一个接口,如下所示:

public interface IFoo
{
     void Foo();
}
Run Code Online (Sandbox Code Playgroud)

你可以正常实现这个:

public class Bar : IFoo
{
     public void Foo() {} // Implicit interface implementation
}
Run Code Online (Sandbox Code Playgroud)

或者,您可以显式实现接口成员,这需要强制转换:

public class Baz : IFoo
{
     void IFoo.Foo() {} // This will require casting the object to IFoo to call
}
Run Code Online (Sandbox Code Playgroud)

这有时非常有用.例如,通常用于IDisposable在首选API调用的类中实现.Close().通过IDisposable显式实现,您可以"隐藏"该Dispose()方法,但仍允许通过using语句使用类实例.