从 Delphi 中的 Class 引用变量访问 Class 常量

Accessing Class Constants from a Class Reference variable in Delphi

我正在使用 Delphi 2007 维护一个旧项目,我在从 Class 引用变量访问 class 常量时遇到问题,我总是得到父 class 常量而不是子项。

假设有一个父 class、一些子 classes、一个 class 引用和最后一个 const 数组来存储 class 引用以用于循环目的。

看看下面的简单程序:

program TestClassConst;

{$APPTYPE CONSOLE}

uses
  SysUtils;

type

  TParent = class
  const
    ClassConst = 'BASE CLASS';
  end;

  TChild1 = class(TParent)
  const
    ClassConst = 'CHILD 1';
  end;

  TChild2 = class(TParent)
  const
    ClassConst = 'CHILD 2';
  end;

  TParentClass = class of TParent;
  TChildClasses = array[0..1] of TParentClass;

const
  ChildClasses: TChildClasses = (TChild1, TChild2);

var
  i: integer;
  c: TParentClass;
  s: string;

begin
  try
    writeln;

    writeln('looping through class reference array');
    for i := low(ChildClasses) to high(ChildClasses) do begin
      c := ChildClasses[i];
      writeln(c.ClassName, ' -> ', c.ClassConst);
    end;

    writeln;

    writeln('accessing classes directly');
    writeln(TChild1.ClassName, ' -> ', TChild1.ClassConst);
    writeln(TChild2.ClassName, ' -> ', TChild2.ClassConst);

  except
    on E: Exception do
      Writeln(E.Classname, ': ', E.Message);
  end;
end.

当它运行时我得到:

looping through class reference array
TChild1 -> BASE CLASS
TChild2 -> BASE CLASS

accessing classes directly
TChild1 -> CHILD 1
TChild2 -> CHILD 2

我希望在数组循环中看到 'CHILD 1' 和 'CHILD 2'!

谁能解释一下为什么它不能与 class 参考一起使用?

无类型的 class 常量是添加了一些作用域的普通常量。
键入的 class 常量实际上是一个无法更改的 class 变量。
问题是 class 变量不是虚拟的。

Hallvard Vassbotn 在这里写了关于这个问题的文章:Part 1, Part 2

您无法从 class 引用访问 class 变量和 class 常量,因为该语言不支持虚拟 class 变量。
当你说 s:= TClass1.SomeConst 时,编译器会在继续编译的其余部分之前将其翻译成 s:= SomeGlobalButHiddenConst

class varclass const只不过是句法糖。
因此 class var/const 和实际 class 之间的 link 仅在编译时存在,它在 运行 时被破坏,很像 [= 中的类型擦除49=]。

RTTI 也没有帮助:Get constant fields from a class using RTTI
我想如果你使用的是 D2007,你唯一的选择是声明一个虚函数,returns 你想要的常量:

D2010 之前的选项:虚方法

TParent = class
  class function Name: string; virtual;
end;

TChild1 = class(TParent)
  class function name: string; override;
....
class function TParent.name: string;
begin
  Result:= Self.ClassConst;
end;

class function TChild1.name: string;
begin
  Result:= Self.ClassConst;   //Silly copy paste solution
end;

这种情况很可悲,但我看不到其他选择。

From Delphi 2010 onwards: use attributes
更好的选择是使用属性,您可以使用 RTTI:

访问这些属性

以下代码有效:

program TestClassConst;

{$APPTYPE CONSOLE}

uses
  SysUtils, rtti;

type

  NameAttribute = class(TCustomAttribute)
  private
    Fname: string;
  public
    constructor Create(const Name: string);
    property Name: string read Fname;
  end;

  [Name('Base class')]
  TParent = class
  const
    ClassConst = 'BASE CLASS';
  private
  public
    class function Name: string;
  end;

  [Name('Child 1')]
  TChild1 = class(TParent)
  const
    ClassConst = 'CHILD 1';
  end;

  [Name('Child 2')]
  TChild2 = class(TParent)
  const
    ClassConst = 'CHILD 2';
  end;

  TParentClass = class of TParent;
  TChildClasses = array[0..1] of TParentClass;

const
  ChildClasses: TChildClasses = (TChild1, TChild2);

var
  i: integer;
  c: TParentClass;
  s: string;

{ TParent }

class function TParent.Name: string;
var
  Context: TRttiContext;
  ClassData: TRttiType;
  Attr: TCustomAttribute;
begin
  Context:= TRttiContext.Create;
  ClassData:= Context.GetType(Self);
  try
    for Attr in ClassData.GetAttributes do begin
      if Attr is NameAttribute then Result:= NameAttribute(Attr).Name;
    end;
  finally
    ClassData.Free;
  end;
end;

{ NameAttribute }

constructor NameAttribute.Create(const Name: string);
begin
  inherited Create;
  FName:= name;
end;

begin
  writeln;

  writeln('looping through class reference array');
  for i := low(ChildClasses) to high(ChildClasses) do begin
    c := ChildClasses[i];
    writeln(c.ClassName, ' -> ', c.Name);
  end;

  writeln;

  writeln('accessing classes directly');
  writeln(TChild1.ClassName, ' -> ', TChild1.Name);
  writeln(TChild2.ClassName, ' -> ', TChild2.Name);
  readln;
end.