It was a long time ago i placed a blog entry. Due to work etc i didn’t had any time for it. But now a new entry from me for the delphi developers under us.
Within delphi 2010 the RTTY RTTI system has been completely renewed allowing us delphi developers to do more with RTTY RTTI. One of the things i do with RTTY RTTI is JSON serialization which is a new feature within delphi 2010. For a quick sample regarding JSON in delphi 2010 i refer to the following site: http://blogs.embarcadero.com/adrian/2009/08/19/json-types-for-server-methods-in-datasnap-2010/
Another new feature in delphi 2010 is custom attributes. With custom attributes you can define some extra information on a property like c#. For example i have a object called foo. This object has a published property called doo. This property i want to give some extra attributes for example a fieldname which will be used in the database for updating. To define such attribute and use it on top of a property i’ll describe in the code snippet below.
//First define the custom attribute(s) you want to have. Within these attributes you can do several
//things. even validation of the field is possible.
//
type TMyAttribute = class(TCustomAttribute)
private
FFieldName : string;
FFieldWidth : Integer;
public
constructor Create(const FieldName : String; FieldWidth: Integer);
property Fieldname : String read FFieldname write FFieldName;
property FieldWidth : Integer read FFieldWidth write FFieldWidth;
end;
//Now we have a attribute defined we create a object which will use that attribute on a property.
//If you take a look at the property Field you notice it on top of that property there is a [TMyAttribute] tag.
//This is how you add a custom attribute on top of a property.
//I also added a property Data without a custom attribute to show the difference.
//
type TFoo = class(TObject)
private
FField : string;
FData: string;
published
[TMyAttribute('VELD1', 60)]
property Field : String read FField write FField;
property Data : string read FData write FData;
end;
type TRttiTest = class(TObject)
public
procedure rttiDemo;
end;
constructor TMyAttribute.Create(const FieldName : String; fieldWidth : integer);
begin
FFieldName := Fieldname;
FFieldWidth := FieldWidth;
end;
//here we are going use RTTY RTTI to go through the properties, check/read the attributes on the property and
//Take some action on it.
//
procedure TRttiTest.rttiDemo;
var
oFoo : TFoo;
oRttiType : TRttiType;
oRttiContext : TRttiContext;
oAttrib : TCustomAttribute;
fFieldName : String;
fFieldWidth : integer;
begin
try
oFoo := TFoo.Create;
oRttiContext := TRttiContext.Create;
oRttiType := oRttiContext.GetType(oFoo.ClassType);
for oAttrib in oRttiType.GetAttributes do
begin
if oAttrib is TMyAttribute then
begin
fFieldName := TMyAttribute(oAttrib).FieldName;
fFieldWidth := TMyAttribute(oAttrib).FieldWidth;
...
end;
end;
finally
FreeAndNil(oFoo);
FreeAndNil(oRttiContext);
end;
end;
As you can see custom attributes can be quite handy. I used it within a application that creates and maintains the database behind it based on the object structure. When a new property is being defined the application automatically creates the new field into the database for me when saving the contents of that object. This system gives me more time for development purposes instead of database management etc. Off course this was my choice of using the attributes system.
Twitter
I am Thijs Lensselink a Webdeveloper from the Netherlands.