Using Virtual Method Interceptors

From RAD Studio
Jump to: navigation, search

Go Up to Attributes (RTTI)

Delphi has a new type in Rtti.pas called System.Rtti.TVirtualMethodInterceptor. Essentially, this type creates a derived metaclass dynamically at run time that overrides every virtual method in the ancestor, by creating a new virtual method table and populating it with stubs that intercept calls and arguments. When the metaclass reference for any instance of the "ancestor" is replaced with this new metaclass, the user can then intercept virtual function calls, change arguments on the fly, change the return value, intercept and suppress exceptions or raise new exceptions, or entirely replace calling the underlying method.

In concept, this is somewhat similar to dynamic proxies from .NET and Java. It is like being able to derive from a class at run time, override methods (but not add new instance fields), and then change the run-time type of an instance to this new derived class.

For more information, see Barry Kelly's blog at

See Also