Delphi-PRAXiS

Delphi-PRAXiS (https://www.delphipraxis.net/forum.php)
-   Sonstige Fragen zu Delphi (https://www.delphipraxis.net/19-sonstige-fragen-zu-delphi/)
-   -   Delphi Wie feststellen ob Property geerbt ist? (https://www.delphipraxis.net/112171-wie-feststellen-ob-property-geerbt-ist.html)

webcss 16. Apr 2008 09:18


Wie feststellen ob Property geerbt ist?
 
Moin moin zusammen,

wie kann ich ermitteln, ob die Property eines TOBject-Nachfahren in diesem definiert wurde oder vom vorfahren geerbt ist? Ich weis
das ich das mittels RTTI rausfinden kann, ich suche allerdings den Punkt wo ich diese Information beziehen kann...

Hat jemand einen Hinweis für mich?

Danke!

sirius 16. Apr 2008 09:44

Re: Wie feststellen ob Property geerbt ist?
 
Über RTTI bekommst du IMHO nur raus, wann das Property published wurde.
Dazu brauchst du Unit TypInfo und IsPublishedProp.

nicodex 16. Apr 2008 10:44

Re: Wie feststellen ob Property geerbt ist?
 
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;

webcss 17. Apr 2008 10:12

Re: Wie feststellen ob Property geerbt ist?
 
Danke für die antworten, hat aber alles nix geholfen weil irgendwie an meiner Frage vorbei.
Hab's wie folgt gelöst:
Delphi-Quellcode:
 if Item.ClassParent.InheritsFrom(TCustomOPFBOItem)
         and IsPublishedProp(Item.ClassParent, PropList[i]^.Name) then
    DoSomthing;


Alle Zeitangaben in WEZ +1. Es ist jetzt 23:03 Uhr.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024 by Thomas Breitkreuz