Delphi 获取特定属性的属性值

Delphi 获取特定属性的属性值,delphi,attributes,rtti,Delphi,Attributes,Rtti,我有一个带有已发布道具的类,我将其序列化为XML MyAttr = class(TCustomAttribute) private FName: string; public constructor Create(const Name: string); property Name: string read FName write FName; end; MyClass = class(TPersistent) private FClassCaption: string; pu

我有一个带有已发布道具的类,我将其序列化为XML

MyAttr = class(TCustomAttribute)
private
  FName: string;
public
  constructor Create(const Name: string);
  property Name: string read FName write FName;
end;

MyClass = class(TPersistent)
private
  FClassCaption: string;
published
  [MyAttr('Class')]
  property ClassCaption: string read FClassCaption write FClassCaption;
end;
由于XML大小至关重要,所以我使用属性来为属性指定较短的名称(即,我无法定义名为“Class”的属性)。 序列化通过以下方式实现:

lPropCount := GetPropList(PTypeInfo(Obj.ClassInfo), lPropList);
for i := 0 to lPropCount - 1 do begin
  lPropInfo := lPropList^[i];
  lPropName := string(lPropInfo^.Name);

  if IsPublishedProp(Obj, lPropName) then begin
    ItemNode := RootNode.AddChild(lPropName);
    ItemNode.NodeValue := VarToStr(GetPropValue(Obj, lPropName, False));
  end;
end;

我需要这样的条件:如果属性标记为MyAttr,则获取“MyAttr.Name”而不是“lPropInfo^.Name”。

您可以使用此函数从给定属性获取属性名(在一分钟内编写,可能需要一些优化):

可以这样称呼:

sAttrName:= GetPropAttribValue(obj.ClassInfo, lPropName);
因此,如果此函数返回空字符串,则表示属性未标记为MyAttr,然后需要使用“lPropInfo^.Name”

sAttrName:= GetPropAttribValue(obj.ClassInfo, lPropName);