English 中文(简体)
特定财产的属性价值
原标题:Get an attribute value of specific property

我有一门带有已出版的推进剂的班子,我将其编成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;

Since XML size is crucial, I use attributes to give shorter name for property (i.e I can t define a property named Class ). Serialization implemented the following way:

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”。

最佳回答

您可以利用这一职能,从特定财产中获取你的属性名称(在一分钟内,可能需要某种优化):

uses
  SysUtils,
  Rtti,
  TypInfo;

function GetPropAttribValue(ATypeInfo: Pointer; const PropName: string): string;
var
  ctx: TRttiContext;
  typ: TRttiType;
  Aprop: TRttiProperty;
  attr: TCustomAttribute;
begin
  Result :=   ;

  ctx := TRttiContext.Create;

  typ := ctx.GetType(ATypeInfo);

  for Aprop in typ.GetProperties do
  begin
    if (Aprop.Visibility = mvPublished) and (SameText(PropName, Aprop.Name)) then
    begin    
      for attr in AProp.GetAttributes do
      begin
        if attr is MyAttr then
        begin
          Result := MyAttr(attr).Name;
          Exit;
        end;
      end;
      Break;
    end;
  end;
end;

要求这样做:

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

因此,如果这一功能空洞,就意味着财产没有在MyAttr的标识,那么你需要使用“lPropInfo^.Name”。

问题回答

暂无回答




相关问题
determining the character set to use

my delphi 2009 app has a basic translation system that uses GNUGetText. i had used some win API calls to prepare the fonts. i thought it was working correctly until recently when someone from Malta ...

Help with strange Delphi 5 IDE problems

Ok, I m going nuts here. For the last (almost) four years, I ve been putting up with some extremely bad behavior from my Delphi 5 IDE. Problems include: Seemingly random errors in coride50.bpl ...

How to write a Remote DataModule to run on a linux server?

i would like to know if there are any solution to do this. Does anyone? The big picture: I want to access data over the web, using my delphi thin clients. But i´would like to keep my server/service ...

How convert string to integer in Oxygene

In Delphi, there is a function StrToInt() that converts a string to an integer value; there is also IntToStr(), which does the reverse. These functions doesn t appear to be part of Oxygene, and I can ...

Quick padding of a string in Delphi

I was trying to speed up a certain routine in an application, and my profiler, AQTime, identified one method in particular as a bottleneck. The method has been with us for years, and is part of a "...

热门标签