Einzelnen Beitrag anzeigen

Benutzerbild von nicodex
nicodex

Registriert seit: 2. Jan 2008
Ort: Darmstadt
286 Beiträge
 
Delphi 2007 Professional
 
#3

Re: Wie feststellen ob Property geerbt ist?

  Alt 16. Apr 2008, 10:44
Wie schon erwähnt, funktioniert das über RTTI nur mit published Properties.
Im folgenden Beispiel wird die Eigenschaft 'Value' in der Basisklasse TFoo nicht gefunden:
Delphi-Quellcode:
uses
  TypInfo;

type
  TFoo = class(TObject)
  private
    FValue: Integer;
  public
    property Value: Integer read FValue;
  end;

type
  TBar = class(TFoo)
  published
    property Value;
  end;

procedure FooBar();
const
  Name = 'Value';
var
  Info: PTypeInfo;
  Data: PTypeData;
begin
  Info := TypeInfo(TBar);
  while Assigned(Info) do
  begin
    ShowMessage(GetEnumName(TypeInfo(TTypeKind), Integer(Info.Kind)) + #10 +
      Info.Name + #10 + '''' + Name + ''' found: ' + BoolToStr(
      (Info.Kind = tkClass) and Assigned(GetPropInfo(Info, Name)), True));
    // Get parent info
    Data := GetTypeData(Info);
    if not Assigned(Data) then
      Info := nil
    else
      case Info.Kind of
        tkSet:
          if Assigned(Data.CompType) then
            Info := Data.CompType^
          else
            Info := nil;
        tkClass:
          if Assigned(Data.ParentInfo) then
            Info := Data.ParentInfo^
          else
            Info := nil;
        tkInterface:
          if Assigned(Data.IntfParent) then
            Info := Data.IntfParent^
          else
            Info := nil;
      else
        Info := nil;
      end;
  end;
end;
  Mit Zitat antworten Zitat