Einzelnen Beitrag anzeigen

Benutzerbild von stahli
stahli

Registriert seit: 26. Nov 2003
Ort: Halle/Saale
4.337 Beiträge
 
Delphi 11 Alexandria
 
#4

AW: RTTI und Generics

  Alt 5. Sep 2011, 10:32
Du kannst mit PropInfo.PropertyType.TypeKind auch prüfen, um was für eine Eigenschaft es sich handelt.


Delphi-Quellcode:
procedure TodProp.ClearPointer(const od: Tod; p: Tod);
var
  Context: TRttiContext;
  RttiType: TRttiType;
  PropInfo: TRttiProperty;
  F: Boolean;
  Attr: TCustomAttribute;
  Value: TValue;
  O: TObject;
  PropValue: String;
begin
  if (not Assigned(od)) or (not Assigned(p)) then
    Exit;
  od.HasNotOdPointer := True;

  Context := TRttiContext.Create;
  RttiType := Context.GetType(od.ClassType);

  if Assigned(RttiType) then
  begin
    for PropInfo in RttiType.GetProperties do
    begin
      F := False;
      for Attr in PropInfo.GetAttributes do
      begin
        if Attr is AttrOd then
          F := True;
      end;
      if F then
      begin
        PropValue := '';
        Value := TValue.Empty;
        case PropInfo.PropertyType.TypeKind of // <<===============
          tkClass:
            begin
              if PropInfo.IsWritable then
              begin
                od.HasNotOdPointer := False;
                Value := PropInfo.GetValue(od);
                if (not Value.IsEmpty) then
                begin
                  O := Value.AsObject;
                  if O = p then
                  begin
                    Value := nil;
                    PropInfo.SetValue(od, Value);
                  end;
                end;
              end;
            end;
          //...
        end;
      end;
    end;
  end;

  Context.Free;
end;
Stahli
http://www.StahliSoft.de
---
"Jetzt muss ich seh´n, dass ich kein Denkfehler mach...!?" Dittsche (2004)
  Mit Zitat antworten Zitat