Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5936623
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T15:25:29+00:00 2026-05-22T15:25:29+00:00

Delphi 2010 introduced custom attributes which can be added to type declarations and methods.

  • 0

Delphi 2010 introduced custom attributes which can be added to type declarations and methods. For which language elements can a custom attribute be used?

The examples which I have found so far include class declarations, fields and methods. (And AFAIK generic classes do not support custom attributes).

Some examples are shown in this article. It looks like variables (external to any class declaration) also can have attributes.

Based on this article, attributes can be used for

  • class and record fields and methods
  • method parameters
  • properties
  • non-local enumeration declarations
  • non-local variable declarations

Are there other language elements where attributes can be placed?


Update: this article indicates that custom attributes can be placed before properties: http://francois-piette.blogspot.de/2013/01/using-custom-attribute-for-data.html

It contains this code example:

type
  TConfig = class(TComponent)
  public
    [PersistAs('Config', 'Version', '1.0')]
    Version : String;
    [PersistAs('Config', 'Description', 'No description')]
    Description : String;
    FTest : Integer;
    // No attribute => not persistent
    Count : Integer;
    [PersistAs('Config', 'Test', '0')]
    property Test : Integer read FTest write FTest;
  end;

I guess that there is also a way to read attributes on method arguments like

procedure Request([FormParam] AUsername: string; [FormParam] APassword: string);
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T15:25:29+00:00Added an answer on May 22, 2026 at 3:25 pm

    Interesting question! You can declare attributes on almost anything, the problem is retrieving them using RTTI. Here’s a quick console demo of declaring custom attributes for:

    • Enums
    • Function type
    • Procedure type
    • Method type (of object)
    • Aliased type
    • Record type
    • Class type
    • Record type that’s internal to a class
    • Record field
    • Record method
    • Class instance field
    • Class class field (class var)
    • Class method
    • Global variable
    • Global function
    • Local variable

    Didn’t find a way to declare a custom attribute for a property of a class. But a custom attribute can be attached to the getter or setter methods.

    Code, the story continues after the code:

    program Project25;
    
    {$APPTYPE CONSOLE}
    
    uses
      Rtti;
    
    type
      TestAttribute = class(TCustomAttribute);
    
      [TestAttribute] TEnum = (first, second, third);
      [TestAttribute] TFunc = function: Integer;
      [TestAttribute] TEvent = procedure of object;
      [TestAttribute] AliasInteger = Integer;
    
      [TestAttribute] ARecord = record
        x:Integer;
        [TestAttribute] RecordField: Integer;
        [TestAttribute] procedure DummyProc;
      end;
    
      [TestAttribute] AClass = class
      strict private
        type [TestAttribute] InnerType = record y:Integer; end;
      private
        [TestAttribute]
        function GetTest: Integer;
      public
        [TestAttribute] x: Integer;
        [TestAttribute] class var z: Integer;
        // Can't find a way to declare attribute for property!
        property Test:Integer read GetTest;
        [TestAttribute] class function ClassFuncTest:Integer;
      end;
    
    var [TestAttribute] GlobalVar: Integer;
    
    [TestAttribute]
    procedure GlobalFunction;
    var [TestAttribute] LocalVar: Integer;
    begin
    end;
    
    { ARecord }
    
    procedure ARecord.DummyProc;
    begin
    end;
    
    { AClass }
    
    class function AClass.ClassFuncTest: Integer;
    begin
    end;
    
    function AClass.GetTest: Integer;
    begin
    end;
    
    begin
    end.
    

    The trouble is retrieving those custom attributes. Looking at the rtti.pas unit, custom attributes can be retrieved for:

    • Record type (TRttiRecordType)
    • Instance type (TRttiInstanceType)
    • Method type (TRttiMethodType)
    • Pointer type (TRttiPointerType) – what’s that used for?
    • Procedure type (TRttiProcedureType)

    There’s no way of retrieving any sort of RTTI for “unit” level or local variables and procedures, hence no way of retrieving information about attributes.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have Delphi 2010. XXX-component uses File_Path:AnsiString. A path can be written in XXX-language.
Delphi 2009 has changed its string type to use 2 bytes to represent a
Delphi 2009, among some cool stuff, has also just got Anonymous methods. I've seen
Delphi 2009 introduced a hierarchical system for project options configuration, where you set base
Delphi 8 introduced Class Helpers for the purposes of mapping the VCL/RTL to the
I use Delphi 2010 and when I try to perform an XmlHttpRequest operation inside
I have a media application (written in Delphi 2010 but I am not sure
I would like to mix MP3 files with Delphi 2010. Is this possible? Does
Possible Duplicate: Embarcadero D2010 hide welcome page I have Delphi 2010. How do I
Possible Duplicate: How to also prepare for 64-bits when migrating to Delphi 2010 and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.