![]() |
Superobject
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. |
AW: Superobject
Maybe its a good advice to have a close look at the sources (superobject.pas)
|
AW: Superobject
-
Delphi-Quellcode:
represents a JSON string
'{value: "hello", test: "hello"}'
- 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; |
Alle Zeitangaben in WEZ +1. Es ist jetzt 14:02 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