AGB  ·  Datenschutz  ·  Impressum  







Anmelden
Nützliche Links
Registrieren
Zurück Delphi-PRAXiS Programmierung allgemein Programmieren allgemein Ini-Einstellung umwandeln für Komponente
Thema durchsuchen
Ansicht
Themen-Optionen

Ini-Einstellung umwandeln für Komponente

Ein Thema von Helmi · begonnen am 10. Mär 2012 · letzter Beitrag vom 11. Mär 2012
Antwort Antwort
Benutzerbild von stahli
stahli

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

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
Benutzerbild von Sir Rufo
Sir Rufo

Registriert seit: 5. Jan 2005
Ort: Stadthagen
9.454 Beiträge
 
Delphi 10 Seattle Enterprise
 
#2

AW: Ini-Einstellung umwandeln für Komponente

  Alt 11. Mär 2012, 10:58
ist ja nicht gerade DRY

Leg dir doch eine interne Procedure an, die lesen und schreiben kann mit einem zusätzlichen Parameter (lesend/schreibend). Mit deinen vorhandenen procedures rufst du dann nur noch diese interne auf und schon ist es DRY und KISS
Kaum macht man's richtig - schon funktioniert's
Zertifikat: Sir Rufo (Fingerprint: ‎ea 0a 4c 14 0d b6 3a a4 c1 c5 b9 dc 90 9d f0 e9 de 13 da 60)
  Mit Zitat antworten Zitat
Antwort Antwort


Forumregeln

Es ist dir nicht erlaubt, neue Themen zu verfassen.
Es ist dir nicht erlaubt, auf Beiträge zu antworten.
Es ist dir nicht erlaubt, Anhänge hochzuladen.
Es ist dir nicht erlaubt, deine Beiträge zu bearbeiten.

BB-Code ist an.
Smileys sind an.
[IMG] Code ist an.
HTML-Code ist aus.
Trackbacks are an
Pingbacks are an
Refbacks are aus

Gehe zu:

Impressum · AGB · Datenschutz · Nach oben
Alle Zeitangaben in WEZ +1. Es ist jetzt 18:30 Uhr.
Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024-2025 by Thomas Breitkreuz