{"id":476,"date":"2009-08-15T19:04:54","date_gmt":"2009-08-15T09:04:54","guid":{"rendered":"http:\/\/www.malcolmgroves.com\/blog\/?p=476"},"modified":"2015-03-13T10:35:59","modified_gmt":"2015-03-12T23:35:59","slug":"rtti-and-attributes-in-delphi-2010","status":"publish","type":"post","link":"http:\/\/www.malcolmgroves.com\/blog\/?p=476","title":{"rendered":"RTTI and Attributes in Delphi 2010"},"content":{"rendered":"<p>RTTI (Runtime Type Information) has had a major overhaul in Delphi 2010. RTTI is a central piece on which the Delphi IDE is written, and as such has been around since the first release, however I\u2019ve heard from a number of people over the years that they\u2019d tried to use RTTI and found it too difficult and arcane, especially compared to the Reflection API\u2019s in Java and .NET. That\u2019s a real shame, as the ability to write code to query the details of other objects, without knowing the type up-front, is really powerful.<\/p>\n<p>However, I think that complaint will be a thing of the past with this new API. It\u2019s been not only extended to make it more capable, it is also now much more approachable and easy to use.<\/p>\n<p>One of the new features that I\u2019m very excited about is support for Attributes in Win32. I\u2019m working on a larger example, but here\u2019s a quick run through of creating and then querying custom attributes.<\/p>\n<p>Custom attributes are simply classes that descend from TCustomAttribute. They can have properties, methods, etc, like any other class:<\/p>\n<pre lang=\"Delphi\">  MyAttribute = class(TCustomAttribute)\r\n  private\r\n    FName: string;\r\n    FAge: Integer;\r\n  public\r\n    constructor Create(const Name : string; Age : Integer);\r\n    property Name : string read FName write FName;\r\n    property Age : Integer read FAge write FAge;\r\n  end;<\/pre>\n<p>No real surprises here, and the constructor is implemented exactly as you\u2019d expect.<\/p>\n<p>Next, we can apply our attribute to our class:<\/p>\n<pre lang=\"Delphi\">  TMyClass = class\r\n  public\r\n    [MyAttribute('Malcolm', 39)]\r\n    procedure MyProc(const s : string);\r\n    [MyAttribute('Julie', 37)]\r\n    procedure MyOtherProc;\r\n  end;<\/pre>\n<p>Here I\u2019ve applied it to both methods, but note I\u2019ve supplied different parameters to the Attribute. Also note the order of the parameters matches the order in the constructor. This is not just limited to methods, you can apply attributes to properties, entire classes, all sorts of things.<\/p>\n<p>Of course, there\u2019s no point adding attributes if you can\u2019t read them from somewhere, so here\u2019s some sample code that uses the new RTTI API to query for our attributes, and display the details in a listbox:<\/p>\n<pre lang=\"Delphi\">procedure TForm3.Button1Click(Sender: TObject);\r\nvar\r\n  ctx : TRttiContext;\r\n  t : TRttiType;\r\n  m : TRttiMethod;\r\n  a : TCustomAttribute;\r\nbegin\r\n  ListBox1.Clear;\r\n\r\n  ctx := TRttiContext.Create;\r\n  try\r\n    t := ctx.GetType(TMyClass);\r\n    for m in t.GetMethods do\r\n      for a in m.GetAttributes do\r\n        if a is MyAttribute then\r\n          ListBox1.Items.Add(Format('Method = %s; Attribute = %s, Name = %s, Age = %d',\r\n                                    [m.Name, a.ClassName, MyAttribute(a).Name,\r\n                                     MyAttribute(a).Age]));\r\n  finally\r\n    ctx.Free;\r\n  end;\r\nend;<\/pre>\n<p>First I grab a reference to the RTTI Context, and load it with the ClassType of my class. I then do a for..in over the methods in my class, and for each method do a for..in over the attributes. Then I check if the attribute is my custom attribute, and if so, grab the values of Name and Age and show them in a list box.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" style=\"margin: 0px 10px 0px 0px; display: inline; border-width: 0px;\" title=\"attribscreenshot\" src=\"http:\/\/www.malcolmgroves.com\/blog\/wp-content\/uploads\/2009\/08\/attribscreenshot1.jpg\" alt=\"attribscreenshot\" width=\"407\" height=\"219\" align=\"left\" border=\"0\" \/><\/p>\n<p>As I mentioned earlier, I\u2019m working on another example which is a little more useful, but hopefully this gives you a little taste of some of the new stuff coming in 2010. Also, don\u2019t forget to watch the preview videos at <a href=\"http:\/\/www.embarcadero.com\/rad-studio-2010\/\">http:\/\/www.embarcadero.com\/rad-studio-2010\/<\/a>, and subscribe to get notified when new ones are published.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>RTTI (Runtime Type Information) has had a major overhaul in Delphi 2010. RTTI is a central piece on which the Delphi IDE is written, and as such has been around since the first release, however I\u2019ve heard from a number of people over the years that they\u2019d tried to use RTTI and found it too [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[98],"tags":[50,19,48,49],"class_list":["post-476","post","type-post","status-publish","format-standard","hentry","category-coding","tag-attributes","tag-delphi","tag-embarcadero","tag-rtti"],"_links":{"self":[{"href":"http:\/\/www.malcolmgroves.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/476","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/www.malcolmgroves.com\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.malcolmgroves.com\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.malcolmgroves.com\/blog\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"http:\/\/www.malcolmgroves.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=476"}],"version-history":[{"count":14,"href":"http:\/\/www.malcolmgroves.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/476\/revisions"}],"predecessor-version":[{"id":1759,"href":"http:\/\/www.malcolmgroves.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/476\/revisions\/1759"}],"wp:attachment":[{"href":"http:\/\/www.malcolmgroves.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=476"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.malcolmgroves.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=476"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.malcolmgroves.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=476"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}