How does a WCF channel (created via ChannelFactory) implement ICommunicationObject, but doesn’t expose the Close() method, for example, unless you cast the proxy to ICommunicationObject? Does that make sense?
I got to thinking about that on the way home today and couldn’t figure it out in my head. Maybe I’m asking the wrong question? Maybe I’m asking a stupid question? 🙂
Is it some kind of ninja trick?
This is done via Explicit Interface Implementation.
Suppose you have an interface, like so:
You can implement this normally:
Alternatively, you can implement the interface members explicitly, which requires the cast:
This can be very useful at times. For example, it is often done to implement
IDisposablein classes where the preferred API would be to call.Close(), for example. By implementingIDisposableexplicitly, you “hide” theDispose()method, but still allow the class instance to be used via a using statement.