The Wiert Corner – irregular stream of stuff

Jeroen W. Pluimers on .NET, C#, Delphi, databases, and personal interests

  • My badges

  • Twitter Updates

  • My Flickr Stream

  • Pages

  • All categories

  • Enter your email address to subscribe to this blog and receive notifications of new posts by email.

    Join 1,406 other followers

Delphi attributes cannot use `const x: array of string` as argument

Posted by jpluimers on 2018/04/12

[WayBack] I want to annotate some of my enumerated types with human facing names. So I want to write: type[Names(‘Explicit time domain’, ‘Implicit time domain’,… – David Heffernan – Google+

This does not work:

constructor Create(const Values: array of string);

It seems to be in QC.

The reason is not obvious until you realise (thanks Stefan for wording that) that attribute constructor arguments are limited to anything that can be const. Open arrays nor dynamic arrays can be const (yes, nowadays you can have “consts” that are dynamic arrays but in fact they are compiler protected variables).

An alternative you might think works, but fails us using one attribute per enumeration element. But that’s where Delphi RTTI leaves you in the dark: it does not expose the RTTI for enumeration elements (likely doesn’t even generate it), only the the enumeration type itself.

So this fails:

type
  NameAttribute = class( TCustomAttribute )
  private
    FName: String;
  public
    constructor Create( AName : String );
    property Name: String read FName;
  end;

  TMyEnumeration = (
    [Name('One')]
    meOne,
    [Name('Two')]
    meTwo
  );

–jeroen

 

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

w

Connecting to %s

 
%d bloggers like this: