There is nothing built-in that allows you to intercept an object that you can not control instantiation of. Similarly, there will be no new facilities for this in the upcoming .net 4.0.
If you can control instantiation:
- If your object can be MarshalByRef you can use RealProxy.
- You could use quite a few IOC containers, eg. LinFu, Castle Dynamic Proxy
- You could use a tool like PostSharp, Mono Cecil or Microsoft CCI to rewrite your assemblies with the interceptions as a post compile step.
If you can not control instantiation
- You can use ICorDebug - the .Net debugging APIs which are really hard to use and heavy.
- You can use ICorProfiler - the .Net profiling APIs where are also pretty complicated to use.
Alternatively, you could look at a dynamic language like IronRuby, which has a built-in alias_method
and define_method
goodness (which allows you to redefine anything), so interception baked in.