Archive
Auto register classes
This is something I wrote back when Attributes was sort of new to Delphi, but it’s a neat example of how custom attributes can simplify your code. It would actually be a nice candidate for addition to the VCL.
In short: If you are serializing objects to JSon, you probably know that Delphi can only re-create those objects if it knows the class type (or if you manually provide the class during parsing). This means that you end up writing an implementation section where you manually call RegisterClass() for each of the class types you use.

While this is not problematic or difficult, it’s one of those chores that is perfect for attributes. So instead of having to write an initialization section on unit level, you can just attach a [ClassRegister] attribute, and it’s automatically registered for you when the unit is loaded into memory.
Here is the unit, feel free to use it and rename it to whatever you like:
unit quartex.util.register;
interface
type
///<summary>
///<para>The [ClassRegister] attribute registers the attached class
/// into Delphi's internal class registry. This is the same as calling
/// RegisterClass manually during unit initialization, except it's
/// simpler and more elegant.</para>
///<code>
///type
/// [ClassRegister]
/// TSomeClass = class(TPersistent)
/// end;
///</code>
///</summary>
ClassRegister = class(TCustomAttribute)
end;
implementation
uses
System.Rtti, System.TypInfo, System.Classes;
// This procedure walks through all classtypes and isolates
// those with our TAutoRegister attribute.
// It then locates the actual classtype and registeres it
// with Delphi's internal persistence layer
procedure ProcessAutoRegisterAttributes;
var
ctx : TRttiContext;
typ : TRttiType;
attr : TCustomAttribute;
lRealType: TClass;
lAccess: PTypeData;
begin
ctx := TRttiContext.Create();
try
for typ in ctx.GetTypes() do
begin
if typ.TypeKind = tkClass then
begin
for attr in typ.GetAttributes() do
begin
if attr is ClassRegister then
begin
lAccess := GetTypeData(typ.Handle);
if lAccess <> nil then
begin
lRealType := lAccess^.ClassType;
if lRealType <> nil then
begin
if lRealType.InheritsFrom(TPersistent)
or lRealType.InheritsFrom(TInterfacedPersistent) then
RegisterClass( TPersistentClass(lRealType) );
end;
break;
end;
end;
end;
end;
end;
finally
ctx.Free();
end;
end;
// We want to register all the classes decorated with our
// attribute when this unit is loaded into memory. This process is
// ultimately very quick since it's all pointer material.
Initialization
begin
ProcessAutoRegisterAttributes;
end;
end.
Calendar
M | T | W | T | F | S | S |
---|---|---|---|---|---|---|
1 | 2 | |||||
3 | 4 | 5 | 6 | 7 | 8 | 9 |
10 | 11 | 12 | 13 | 14 | 15 | 16 |
17 | 18 | 19 | 20 | 21 | 22 | 23 |
24 | 25 | 26 | 27 | 28 | 29 | 30 |
31 |
Recent
The vatican vault
- March 2023
- February 2023
- December 2022
- October 2022
- January 2022
- October 2021
- March 2021
- November 2020
- September 2020
- July 2020
- June 2020
- April 2020
- March 2020
- February 2020
- January 2020
- November 2019
- October 2019
- September 2019
- August 2019
- July 2019
- June 2019
- May 2019
- April 2019
- March 2019
- February 2019
- January 2019
- December 2018
- November 2018
- October 2018
- September 2018
- August 2018
- July 2018
- June 2018
- May 2018
- April 2018
- March 2018
- February 2018
- January 2018
- December 2017
- November 2017
- October 2017
- August 2017
- July 2017
- June 2017
- May 2017
- April 2017
- March 2017
- February 2017
- January 2017
- December 2016
- November 2016
- October 2016
- September 2016
- August 2016
- July 2016
- June 2016
- May 2016
- April 2016
- March 2016
- January 2016
- December 2015
- November 2015
- October 2015
- September 2015
- August 2015
- June 2015
- May 2015
- April 2015
- March 2015
- February 2015
- January 2015
- December 2014
- November 2014
- October 2014
- September 2014
- August 2014
- July 2014
- June 2014
- May 2014
- April 2014
- March 2014
- February 2014
- January 2014
- December 2013
- November 2013
- October 2013
- September 2013
- August 2013
- July 2013
- June 2013
- May 2013
- February 2013
- August 2012
- June 2012
- May 2012
- April 2012
You must be logged in to post a comment.