Delphi Attributes & AOP

From Passive Metadata to Runtime Interception

Architecture Guide

You asked about the limits of Attributes in Delphi compared to C#. The short answer: Delphi attributes are purely passive RTTI metadata. They do not alter class behavior natively. However, by combining them with TVirtualInterface and runtime interception, you can achieve Attribute-Oriented Programming (AOP).

The Fundamental Limit

In C# (especially with tools like PostSharp) or Java (Spring AOP), attributes can trigger code injection at compile-time or load-time. In Delphi, attributes are strictly information. They sit in the binary, waiting to be read.

Delphi Native Behavior

Passive

[MyAttribute]

Just data compiled into the DCU/EXE.

⬇️

Method Code

Executes normally. Ignores the attribute completely unless asked.

AOP (Achievable via Frameworks)

Active

[LogExecution]

Intercepts the call *before* it hits the object.

⬇️

Interceptor Logic

Executes Pre-Processing -> Calls Method -> Post-Processing.

No Compile-Time Injection

Delphi compilers do not support "weaving" (modifying IL/Assembly during build) based on attributes out of the box.

Runtime Only

All attribute discovery happens at runtime using System.Rtti. This adds a performance overhead.

Interface Based

To truly change behavior, you essentially must use Interfaces + TVirtualInterface to wrap the target class.