Einzelnen Beitrag anzeigen

Benutzerbild von stahli
stahli

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

AW: Ini-Einstellung umwandeln für Komponente

  Alt 11. Mär 2012, 10:46
Über die RTTI kannst du diese ENUMs auch direkt in Strings umwandeln und zurück.
Oder du nimmst einfache Casts ala Ord, und konvertierst die ENUMs in Integer.
So habe ich das gemacht. Das ist nicht der schnellste Weg und etwas aufwendig umzusetzen, aber es ist dann dafür universell einsetzbar für alle Propertys.
Falls jemand mal nachschauen möchte:

Delphi-Quellcode:
procedure TodProp.LoadPropValue(const od: Tod; PropName, PropValue: String);
var
  Context: TRttiContext;
  RttiType: TRttiType;
  PropInfo: TRttiProperty;
  F: Boolean;
  Attr: TCustomAttribute;
  Value: TValue;
  InstOd: TInstOd;
  _od, iod: Tod;
  _PropName: String;
  V: Integer;
begin
  if (not Assigned(od)) or (PropName = '') then
    Exit;

  _od := od;
  _PropName := PropName;
  CorrectSubOd(_od, _PropName);
  if not Assigned(_od) then
    Exit;

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

  if Assigned(RttiType) then
    begin
      for PropInfo in RttiType.GetProperties do
        begin
          if PropInfo.Name <> _PropName then
            Continue;
          F := False;
          for Attr in PropInfo.GetAttributes do
            begin
              if Attr is AttrOd then
                F := True;
            end;
          if F then
            begin
              Value := TValue.Empty;
              case PropInfo.PropertyType.TypeKind of
                tkUnknown:
                  ;
                tkInteger:
                  Value := TValue.From(StrToIntDef(PropValue, 0));
                tkChar:
                  ;
                tkEnumeration: // <<<<<================================= HIER
                  if TryStrToInt(PropValue, V) then
                    Value := TValue.FromOrdinal(PropInfo.PropertyType.Handle, V)
                  else
                    Value := TValue.FromOrdinal(PropInfo.PropertyType.Handle, GetEnumValue(PropInfo.PropertyType.Handle, PropValue));
                tkFloat:
                  if PropInfo.GetValue(_od).IsType<TDateTime> then
                    Value := TValue.From(StrToDateTimeDef(PropValue, 0))
                  else if PropInfo.GetValue(_od).IsType<TTime> then
                    Value := TValue.From(StrToTimeDef(PropValue, 0))
                  else
                    Value := TValue.From(StrToFloatDef(PropValue, 0));
                tkString:
                  Value := TValue.From(PropValue);
                tkSet:
                  ;
                tkClass:
                  begin
                    iod := FoundOd(PatternToId(PropValue));
                    if (Assigned(iod)) or (AssignInstOdFlag) then
                      begin
                        Value := iod;
                      end
                    else
                      begin
                        InstOd := TInstOd.Create;
                        InstOd.od := _od;
                        InstOd.PropName := _PropName;
                        InstOd.Id := PropValue;
                        InstOdList.Add(InstOd);
                      end;
                  end;
                tkMethod:
                  ;
                tkWChar:
                  ;
                tkLString:
                  ;
                tkWString:
                  ;
                tkVariant:
                  ;
                tkArray:
                  ;
                tkRecord:
                  ;
                tkInterface:
                  ;
                tkInt64:
                  ;
                tkDynArray:
                  ;
                tkUString:
                  Value := TValue.From(PropValue);
                tkClassRef:
                  ;
                tkPointer:
                  ;
                tkProcedure:
                  ;
              end;
              if not Value.IsEmpty then
                PropInfo.SetValue(_od, Value);
            end;
        end;
    end;

  Context.Free;
end;

function TodProp.GetPropValue(const od: Tod; PropName: String): String;
var
  PropValue: String;
  Context: TRttiContext;
  RttiType: TRttiType;
  PropInfo: TRttiProperty;
  F: Boolean;
  Attr: TCustomAttribute;
  Value: TValue;
  O: TObject;
  _od: Tod;
  _PropName: String;
begin
  Result := '';

  if (not Assigned(od)) or (PropName = '') then
    Exit;

  if Lowercase(PropName) = Lowercase('odId') then
    Exit(od.odId);

  _od := od;
  _PropName := PropName;
  CorrectSubOd(_od, _PropName);
  if not Assigned(_od) then
    Exit;

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

  if Assigned(RttiType) then
    begin
      for PropInfo in RttiType.GetProperties do
        begin
          if PropInfo.Name <> _PropName then
            Continue;
          F := False;
          for Attr in PropInfo.GetAttributes do
            begin
              if Attr is AttrOd then
                F := True;
            end;
          if F then
            begin
              PropValue := '';
              Value := PropInfo.GetValue(_od);
              case Value.Kind of
                tkUnknown:
                  ;
                tkInteger:
                  if Value.AsInteger = 0 then
                    PropValue := ''
                  else
                    PropValue := inttostr(Value.AsInteger);
                tkChar:
                  ;
                tkEnumeration: // <<<<<================================= HIER
                  if Value.AsOrdinal = 0 then
                    PropValue := ''
                  else
                    PropValue := GetEnumName(Value.TypeInfo, Value.AsOrdinal);
                tkFloat:
                  if Value.AsExtended = 0 then
                    PropValue := ''
                  else if Value.IsType<TDateTime> then
                    PropValue := DateTimeToStr(Value.AsExtended)
                  else if Value.IsType<TTime> then
                    PropValue := TimeToStr(Value.AsExtended)
                  else
                    PropValue := FloatToStr(Value.AsExtended);
                tkString:
                  PropValue := Value.AsString;
                tkSet:
                  ;
                tkClass:
                  begin
                    O := Value.AsObject;
                    try
                      if (O <> nil) and (O is Tod) then
                        PropValue := (O as Tod).odId;
                    except
                      PropValue := '';
                    end;
                  end;
                tkMethod:
                  ;
                tkWChar:
                  ;
                tkLString:
                  ;
                tkWString:
                  ;
                tkVariant:
                  ;
                tkArray:
                  ;
                tkRecord:
                  ;
                tkInterface:
                  ;
                tkInt64:
                  ;
                tkDynArray:
                  ;
                tkUString:
                  PropValue := Value.AsString;
                tkClassRef:
                  ;
                tkPointer:
                  ;
                tkProcedure:
                  ;
              end;
              if PropValue <> 'then
                Result := PropValue;
            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