So how does CodeSiteEx manage to do this, you may ask? The magic behind this relies heavily on how the Delphi compiler handles interfaces. Interfaces are an interesting language feature of Delphi that are useful in several ways. Probably the most handy (and irritating) feature of interfaces is that they are reference-counted. When an interface reference to an object is created, Delphi's compiler automatically inserts code to increment its reference count and when that interface reference goes out of scope or is set to nil, the compiler inserts code to decrement its reference count. When the reference count reaches zero, the object that the interface refers to is destroyed.
CodeSiteEx takes advantage of this "compiler magic" to make the method-tracing system work. TraceMethod is a function that returns a very simple interface; in fact, it is so simple that it doesn't have any properties or methods! But when the compiler's code to increment the interface's reference count is called, the object that the interface refers to calls EnterMethod. Then, when the method ends and the interface goes out of scope, the code to decrement its reference is automatically executed and ExitMethod is called. Very neat and tidy.
However, there is one more piece of Delphi "compiler magic" that CodeSiteEx depends on. Normally, when a function is called there is a variable ready to store its result. But if a function is called without such a variable, the compiler is forced to create a temporary variable to store the returned value. This behavior is very useful to CodeSiteEx because it means that the compiler will automatically create a variable to store the returned interface until it goes out scope. This very powerfully turns what was once five lines of code into one.