Blog
All Blog Posts | Next Post | Previous Post#WEBWONDERS: Using Object Pascal RTTI in TMS WEB Core projects
Friday, September 1, 2023
The aim of TMS WEB Core was from the beginning to offer as familiar web client application development as Object Pascal RAD component based development is with the VCL for Windows native apps. This means being hosted in the familiar Delphi IDE, have the same workflow as building VCL apps from the Delphi IDE, use components that are as similar as possible as VCL counterparts and of course, also ensure that the Object Pascal language that can be used matches as close as possible the latest Delphi language features.
RTTI
And yes, even when the Object Pascal language in your app is transpiled to JavaScript to run in the browser, advanced language features such as RTTI can also be used for TMS WEB Core web client apps. Originated from a customer support question, this blog covers how you can use advanced RTTI. .
The challenge
First of all, note that the RTL code that can be used to deal with RTTI in TMS WEB Core is designed to match as closely as possible with how you would write the code for a Delphi VCL app. For this blog challenge, the task was to retrieve and call class methods with parameters using RTTI. Well, the challenge isn't much of a challenge as the code is identical to how you would do this in a VCL app. We start by adding the RTTI unit to the uses clause and then use an RTTIContext to get a class type, query its methods and finally invoke the method.
The code becomes:
uses RTTI; procedure TForm1.WebButton1Click(Sender: TObject); var RttiContext: TRttiContext; RttiType: TRttiType; Method: TRttiMethod; MyClass: TPersistentClass; s,i,d: TValue; begin MyClass := GetClass('TForm1'); if Assigned(MyClass) then begin RttiContext := TRttiContext.Create; try RttiType := RttiContext.GetType(MyClass); Method := RttiType.GetMethod('Test'); if Assigned(Method) then begin s := TValue.From('Hello world'); i := TValue.From(Random(20)); d := TValue.From(Now); Method.Invoke(Self, [s,i,d]); end; finally RttiContext.Free; end; end; end;
In this case, the method to be invoked is declared on the form class from TForm1 and it uses the instance of this form itself (Self) to invoke the method on.
The method has 3 parameters, a string, integer and a date to show how you can pass a variable number of different parameter types.
This Test method is declared as:
type TForm1 = class(TWebForm) procedure Test(AMessage: string; AValue: integer; ADate: TDateTime); end;
and the implementation is simply:
procedure TForm1.Test(AMessage: string; AValue: integer; ADate: TDateTime); begin ShowMessage('Message ' + AValue.ToString +':' + AMessage + #13#10 'received on ' + TimeToStr(ADate)); end;
Limitations
Conclusion
You can see that being a TMS WEB Core developer gives you a tremendous advantage to reuse all the Object Pascal developing experience you accumulated in the past 28 years and use it to your benefit to create zero deploy rich web client apps. You could use RTTI as well to retrieve and set properties of classes and so much more.
Bruno Fierens
This blog post has not received any comments yet.
All Blog Posts | Next Post | Previous Post