All Blog Posts  |  Next Post  |  Previous Post

#WEBWONDERS: Using Object Pascal RTTI in TMS WEB Core projects


Friday, September 1, 2023

TMS Software Delphi  Components

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.


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:


procedure TForm1.WebButton1Click(Sender: TObject);
  RttiContext: TRttiContext;
  RttiType: TRttiType;
  Method: TRttiMethod;
  MyClass: TPersistentClass;
  s,i,d: TValue;
  MyClass := GetClass('TForm1');
  if Assigned(MyClass) then
    RttiContext := TRttiContext.Create;
      RttiType := RttiContext.GetType(MyClass);
      Method := RttiType.GetMethod('Test');
      if Assigned(Method) then
        s := TValue.From('Hello world');
        i := TValue.From(Random(20));
        d := TValue.From(Now);
        Method.Invoke(Self, [s,i,d]);

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:

  TForm1 = class(TWebForm)
    procedure Test(AMessage: string; AValue: integer; ADate: TDateTime);

and the implementation is simply:

procedure TForm1.Test(AMessage: string; AValue: integer; ADate: TDateTime);
  ShowMessage('Message ' + AValue.ToString +':' + AMessage + #13#10 'received on ' + TimeToStr(ADate));


What always will remain a limitation is dealing with pointers. With JavaScript code running in the browser, there is no such concept of a pointer. Hence, it is not possible to use pointers in combination with RTTI. Fortunately, in most clean code these days, pointers are rarely needed. 


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.

Add a new comment

You will receive a confirmation mail with a link to validate your comment, please use a valid email address.
All fields are required.

All Blog Posts  |  Next Post  |  Previous Post