One of the commonly asked questions about Castle Dynamic Proxy:
How do I use CreateClassProxy to wrap an existing object to intercept calls to its virtual members?
Short answer is – you can’t. And if you think about it, there’s a very good reason for that.
Since classes are stateful, and there’s no requirement that all their members are virtual, you could end up with inconsistent state, where part of the state comes from proxy object, part comes from the target object. This would lead to bizarre behavior and bugs, that would be hard to catch. For that reason Dynamic Proxy does not expose this functionality.
You can either expose the required set of API as interface and create interface proxy with target interface, or to create a wrapper class.
If that’s not clear enough, consider the following example of how such proxy could look like and behave:
As you can see here, we have a class which has some state in it. We then create a proxy, and call some methods. The important thing is the last assert. Proxy returns invalid results to us – we have a bug! While the proxy should be transparent, and behave as if it was the joe himself, it returns false values to us. This is not something that can be worked around, hence no class proxies with targets.