You can use the dependency injection infrastructure such as Unity, Ninject, Castle Windsor and implement Interceptor. The task of the interceptor is to intercept the call to something, for example a method, and do something like registration with it.
fejesjoco mentioned dynamic proxies, and they are used by Castle Windsor to intercept method calls.
This is a great topic for this format, but if you're interested, go to Google ninject and follow the instructions on that. How large software systems come together to be flexible and provide future change.
source share