Delphi 2007 Handbook




Essential Pascal




social web book








April 4, 2008

Dynamic Methods Invocation in Delphi (Part 4)

I've found some time to improve the source code posted earlier.

Yesterday, while traveling in train to Rome, I've found some time to improve the source code posted earlier for dynamic methods invocation. The code is now close to the point of being reusable and very easy to use. But it is still not complete... here is the description of the idea, I'll post the new complete code at a later time (or possibly put it in some SVN repository) and also get back to discuss why and when you want to write code like this. One thing at a time.

First of all, the code I showed earlier made an explicit use of variants and this is far from great. It also looked very little object oriented. What I've done in this new version is embed the custom variant in a class, so that you access the dynamic features with a special purpose variant property. For example, you can now write:

var
  md: TMyDate;
begin
  md := TMyDate.Create;
  md.v.NextMonday;

In practice with this code you create an instance of a regular class with some data, some methods, plus some dynamic methods. In this case the "v" property is used to access to the dynamic features of the class.  Now, I tried to find a better identifier. Stretching Object Pascal rules a bit (what is a character in an identifier?) I came up with the following alternatives (using keys on my keyboard):

md.§.NextMonday;
md.&do.dec24;
md.°.NextMonday;
md.€.NextMonday;

I thought "do" would have been nice but the need for an extra ampersand makes it very hard to pick. Any ideas?

Anyway, the beauty of this approach compared to the previous one is that now the data you are accessing dynamically is plain data with the class (TMyDate). Only, you have to inherit it from a base dynamic variant support class and provide a proper "DoProcedure" to it. In a few minutes you can have a class with a partial dynamic interface... but for the full code you'll have to wait (still needs a couple of checks). This is the TMyDate definition:

type
  TMyDate = class (TDynamicClass)
  private
    fDate: TDateTime;
  public
    constructor Create;
    function DoProcedure(const V: TVarData; const Name: string;
      const Arguments: TVarDataArray): Boolean; override;
    function AsString: string; override;
  end;

It will be that simple to have your dynamic data type... because Delphi rules!





 

5 Comments

Dynamic Methods Invocation in Delphi Part 4 

 Hi Marco,
     My recommendation (even though it is a little 
long) would be "Invoke", e.g., MD.Invoke.NextMonday;
I don't like any of the special characters and "v" 
has no meaning.
Tom
Comment by Thomas Grubb on April 4, 22:36

Dynamic Methods Invocation in Delphi Part 4 

Hello, 

Great idea, but I would suggest implementing an 
interface

IDynamicClass

where the IDynamicClass is delegated to an object, 
TDynamicObject. The TDynamicClass communicates back 
to the owner via an event. 

That way, I don't have to inherit from TDynamicClass, 
I can make any existing class I have support dynamic 
methods without changing the inheritance.
Comment by David Novo [http://www.denovosoftware.com] on April 5, 04:11

Dynamic Methods Invocation in Delphi Part 4 

<quote>
Yeah, but your scientists were so preoccupied with
whether or not they could, they didn't stop to think
if they should. 
</quote>

Is NOBODY ever going to stop and explain WHY this is
useful?

Hats off and a round of applause for the cleverness,
but I cannot think of a single instance where being
able to do this would give any benefit or advantage.

What am I missing?
Comment by Jolyon Smith [http://www.deltics.co.nz] on April 7, 05:44

Dynamic Methods Invocation in Delphi Part 4 

Use simple '_' character. It's prefix for special
functions in Delphi... and yours TDynamicClass can use
"Special" dynamics functions, declared by user :)

it's look nice:

MyClass._.DynFunction('bla');
Comment by BadEnglish on April 9, 00:47

Dynamic Methods Invocation in Delphi Part 4 

when and where you will post the last sources Cantu???
Comment by Marcos Barreto on June 14, 21:25


Post Your Comment

Click here for posting your feedback to this blog.

There are currently 0 pending (unapproved) messages.