AGB  ·  Datenschutz  ·  Impressum  







Anmelden
Nützliche Links
Registrieren
Thema durchsuchen
Ansicht
Themen-Optionen

Superobject

Ein Thema von Ryzinski · begonnen am 8. Okt 2012 · letzter Beitrag vom 8. Okt 2012
Antwort Antwort
Ryzinski

Registriert seit: 2. Okt 2012
38 Beiträge
 
#1

Superobject

  Alt 8. Okt 2012, 20:44
Hello!

Is there a more native way to do this in XE3? Without the superobject library?

Delphi-Quellcode:
unit Unit4;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs,superobject, Vcl.StdCtrls;

type
  TForm4 = class(TForm)
    Button1: TButton;
  procedure TestMethod(value,test: string);
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form4: TForm4;

implementation

{$R *.dfm}
procedure TForm4.Button1Click(Sender: TObject);
begin
Label1.Caption:=SOInvoke(Self, 'TestMethod', SO('{value: "hello",test: "hello"}')).AsString;
end;

procedure TForm4.TestMethod(value,test: string);
begin
  Caption := value;
  Button1.Caption:=test;
  result:='blabla';
end;
end.

Geändert von Ryzinski ( 8. Okt 2012 um 20:53 Uhr)
  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: Superobject

  Alt 8. Okt 2012, 21:44
Maybe its a good advice to have a close look at the sources (superobject.pas)
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
Benutzerbild von himitsu
himitsu

Registriert seit: 11. Okt 2003
Ort: Elbflorenz
43.149 Beiträge
 
Delphi 12 Athens
 
#3

AW: Superobject

  Alt 8. Okt 2012, 23:50
- '{value: "hello", test: "hello"}' represents a JSON string

- convert JSONString to JSONObject

- convert values of JSONObject to array of TValue

- TRttiMethod.Invoke to call the method

untested code:
Delphi-Quellcode:
uses System.RTTI, Data.DBXJSON, Data.DBXJSONReflect;

type
  TJSONUnMarshal2 = class(TJSONUnMarshal); // hack for protected JSONToTValue

var
  MethodName, ParamStr: string;
  Method: TRttiMethod;
  MethodParams: TArray<TRttiParameter>;
  JSONValues: TJSONValue;
  JSONPair: TJSONPair;
  JSONConvert: TJSONUnMarshal2;
  Values: TArray<TValue>;
  i: Integer;
begin
  MethodName := 'TestMethod';
  ParamStr := '{value: "hello", test: "hello"}'; // '{}' if none Parameters exists

  Method := TRttiContext.Create.GetType(Self.ClassType).GetMethod(MethodName);
  if not Assigned(Method) then
    raise Exception.CreateFmt('Method "%s" does not exists.', [MethodName]);
  MethodParams := Method.GetParameters;

  JSONValues := TJSONObject.ParseJSONValue(ParamStr);
  if not (JSONValues is TJSONObject) then
    raise Exception.CreateFmt('Invalid Parameters for Method "%s".', [MethodName]);
  if TJSONObject(JSONValues).Size <> Length(MethodParams) then
    raise Exception.CreateFmt('Invalid Parameter-Count for Method "%s".', [MethodName]);
  JSONConvert := TJSONUnMarshal2(TJSONConverters.GetJSONUnMarshaler);
  SetLength(Values, TJSONObject(JSONValues).Size);
  for i := 0 to High(Values) do begin
    JSONPair := TJSONObject(JSONValues).Get(i);
    if not SameText(JSONPair.JsonString.Value, MethodParams[i].Name) then
      raise Exception.CreateFmt('Invalid Parameter "%s" for Method "%s".', [MethodParams[i].Name, MethodName]);
    Values[i] := JSONConvert.JSONToTValue(JSONPair.JsonValue, MethodParams[i].ParamType);
  end;

  Result := Method.Invoke(Self, Values).AsString;
end;
Garbage Collector ... Delphianer erzeugen keinen Müll, also brauchen sie auch keinen Müllsucher.
my Delphi wish list : BugReports/FeatureRequests
  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 02:16 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