How does ChannelFactory <T> .CreateChannel work?
If I have an interface:
public interface ISomething { void DoAThing(); }
Then I create an instance with ChannelFactory:
var channel = new ChannelFactory<ISomething>().CreateChannel
I get an instance that I can use.
Now, to close it, I need to click:
((IClientChannel)channel).Close
or
((IChannel)channel).Close
or
((ICommunicationObject)channel).Close
My ISomething interface does not inherit any of these interfaces.
So, which object did the CreateChannel method return and how did he build a dynamic object that could implement an interface that he did not know about before the run-time expired?
ChannelFactory.CreateChannel () returns an implementation of RealProxy , which is part of a set of tools commonly called TransparentProxy or "Remoting", which is a slightly outdated pre-wcf technology. To create an actual class that implements the interface, it boils down to an internal structure level method called RemotingServices .CreateTransparentProxy (...), which I have not looked at, but which is most likely the creator / emitter of the class.
As you ask, you can do something like this. To implement an interface at runtime, I recommend the Dynamic Proxy Lock , which implements interfaces or an abstract class effortlessly.