Einzelnen Beitrag anzeigen

TiGü

Registriert seit: 6. Apr 2011
Ort: Berlin
3.070 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