AGB  ·  Datenschutz  ·  Impressum  







Anmelden
Nützliche Links
Registrieren
Zurück Delphi-PRAXiS Programmierung allgemein Programmieren allgemein Delphi Tvalue in unterschieldiche Typen von TRemotableXS wandeln

Tvalue in unterschieldiche Typen von TRemotableXS wandeln

Ein Thema von fisipjm · begonnen am 10. Mai 2022 · letzter Beitrag vom 10. Mai 2022
Antwort Antwort
fisipjm

Registriert seit: 28. Okt 2013
245 Beiträge
 
#1

Tvalue in unterschieldiche Typen von TRemotableXS wandeln

  Alt 10. Mai 2022, 08:13
Hi,

ich versuche gerade eine über WSDL importierte Klasse dynamisch auszulesen.
Sagen wir ich hab die Klasse und durch die WSDL werden mir 200 Properties erzeugt die ich auslesen kann, ich hab aber 50 verschiedene WSDLs, bin aber schreibfaul deshalb will ich nicht jede einzelne property ausschreiben, sondern über die RTTI dynamisch laden lassen. Würde tasächlich auch schon klappen, wenn die Properties nicht vom Typ TRemotableXS wären (TXSstring, TXSFloat, TXSBoolean....). Ich bekomme aktuell die Pointer als String, hab aber keinen Peil wie ich die Zuweisung richtig machen kann. Folgenden Code hab ich schon. Zugegeben Copy Past aus Stackoverflow, aber ich verstehe was er tut

Delphi-Quellcode:
uses
 ...System.TypInfo, system.Rtti;

procedure TForm5.SpeedButton1Click(Sender: TObject);
var
  I: Word;
  lstringlist: TStringList;
  lExport: TMyPropFullClass;
begin
  ...
  for I := 0 to High(lExport.Data) - 1 do
  begin
    GetObjectProperties(lZeitExport.Data[I], lstringlist)
  end;
  ...
end;


procedure TForm.GetObjectProperties(AObject: TObject; AList: TStrings);
var
  ctx: TRttiContext;
  rType: TRttiType;
  rProp: TRttiProperty;
  AValue: TValue;
  sVal: string;
const
  SKIP_PROP_TYPES = [tkUnknown, tkInterface];
begin
  if not Assigned(AObject) and not Assigned(AList) then
    Exit;

  ctx := TRttiContext.create;
  rType := ctx.GetType(AObject.ClassInfo);
  for rProp in rType.GetProperties do
  begin
    if (rProp.IsReadable) and not(rProp.PropertyType.TypeKind in SKIP_PROP_TYPES) then
    begin
      AValue := rProp.GetValue(AObject);
      if AValue.IsEmpty then
      begin
        sVal := 'nil';
      end
      else
      begin
        if AValue.Kind in [tkUString, tkString, tkWString, tkChar, tkWChar] then
          sVal := QuotedStr(AValue.ToString)
        else
          sVal := AValue.ToString;
      end;

      AList.Add(rProp.Name + '=' + sVal);
    end;

  end;
end;
Die Ausgabe sieht dann aktuell so aus:
Code:
name=(TXSString @ 000000000854C700)
Nicht so dolle, ist aber auch klar, weil TValue.ToString ja keinen Plan von meiner TRemotableXS Klasse hat, ich haber aber keine Idee wie ich ihm beibringen soll das es ein TRemotableXS ist. Wie ich einen String in ein TRemotableXS bekomme weis ich, aber das ist ja leider nicht was hier gerade notwendig wäre. Ich bin auch noch relativ neu in der RTTI Thematik, von dem her hoffe ich auf den ein oder anderen Kniff eines alten Hasen

Grüße
PJM
  Mit Zitat antworten Zitat
TiGü

Registriert seit: 6. Apr 2011
Ort: Berlin
3.058 Beiträge
 
Delphi 10.4 Sydney
 
#2

AW: Tvalue in unterschieldiche Typen von TRemotableXS wandeln

  Alt 10. Mai 2022, 09:26
Delphi-Quellcode:
procedure {TForm.}GetObjectProperties(AObject: TObject; AList: TStrings);
var
  ctx: TRttiContext;
  rType: TRttiType;
  rProp, rProp2: TRttiProperty;
  rInstance: TRttiInstanceType;
  AValue: TValue;
  sVal: string;
const
  SKIP_PROP_TYPES = [tkUnknown, tkInterface];
begin
  if not Assigned(AObject) and not Assigned(AList) then
    Exit;

  ctx := TRttiContext.create;
  rType := ctx.GetType(AObject.ClassInfo);
  for rProp in rType.GetProperties do
  begin
    if (rProp.IsReadable) and not(rProp.PropertyType.TypeKind in SKIP_PROP_TYPES) then
    begin
      if rProp.PropertyType.TypeKind = tkClass then
      begin
        rInstance := rProp.PropertyType.AsInstance;
        AValue := rProp.GetValue(AObject);
        if AValue.IsInstanceOf(TRemotableXS) then
        begin
          sVal := TRemotableXS(AValue.AsObject).NativeToXS;
        end;
      end else
      begin
        AValue := rProp.GetValue(AObject);
        if AValue.IsEmpty then
        begin
          sVal := 'nil';
        end
        else
        begin
          if AValue.Kind in [tkUString, tkString, tkWString, tkChar, tkWChar] then
            sVal := QuotedStr(AValue.ToString)
          else
            sVal := AValue.ToString;
        end;
      end;

      AList.Add(rProp.Name + '=' + sVal);
    end;
  end;
end;

type
  TMyPropFullClass = class
  private
    FSomeString: TXSstring;
  public
    constructor Create;
    property SomeString: TXSstring read FSomeString;
  end;

constructor TMyPropFullClass.Create;
begin
  inherited;
  FSomeString := TXSstring.Create;
  FSomeString.XSToNative('Hello World');
end;

initialization
var MyData: TMyPropFullClass := TMyPropFullClass.Create;
var MyList: TStringList := TStringList.Create;
  GetObjectProperties(MyData, MyList);
  OutputDebugString(PChar(MyList.Text));
So funktioniert das für mich und den unteren kleinen Beispielcode.
  Mit Zitat antworten Zitat
fisipjm

Registriert seit: 28. Okt 2013
245 Beiträge
 
#3

AW: Tvalue in unterschieldiche Typen von TRemotableXS wandeln

  Alt 10. Mai 2022, 10:41
So funktioniert das auch für mich.
Besten Dank
  Mit Zitat antworten Zitat
Themen-Optionen Thema durchsuchen
Thema durchsuchen:

Erweiterte Suche
Ansicht

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 21:57 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