Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jpluimers/472c06440e39ba7ca930cdf8934585ab to your computer and use it in GitHub Desktop.
Save jpluimers/472c06440e39ba7ca930cdf8934585ab to your computer and use it in GitHub Desktop.
Distinct type types in Delphi
program RttiForDistinctTypedTypesConsoleProject;
{$APPTYPE CONSOLE}
{$R *.res}
uses
// System,
System.Classes,
System.SysUtils,
System.Rtti,
System.UITypes;
// Dump based on https://stackoverflow.com/questions/7836880/how-can-i-distinguish-tdatetime-properties-from-double-properties-with-rtti
// The easiest to dump RTTI is by using a construct that:
// 1. gives you a TypeInfo type information handle, like a class having a ClassInfo function.
// 2. allows easy enumeration (again a class)
type
TInstance = class
strict private
FIntegerField: Integer;
FColorField: TColor;
FDoubleField: Double;
FDateTimeField: TDateTime;
FDateField: TDate;
FTimeField: TTime;
FStringField: string;
FFontNameField: TFontName;
public
property IntegerProperty: Integer read FIntegerField write FIntegerField;
property ColorProperty: TColor read FColorField write FColorField;
property DoubleProperty: Double read FDoubleField write FDoubleField;
property DateTimeProperty: TDateTime read FDateTimeField write FDateTimeField;
property DateProperty: TDate read FDateField write FDateField;
property TimeProperty: TTime read FTimeField write FTimeField;
property StringProperty: string read FStringField write FStringField;
property FontNameProperty: TFontName read FFontNameField write FFontNameField;
end;
type
TMarkdownSeparator = (Line, Row);
procedure MarkdownDump(
const aMarkdownSeparator: TMarkdownSeparator;
const aName: string;
const aType: string;
const aQualifiedType: string;
const aKind: string);
var
FixString: string;
SeparatorString: string;
begin
case aMarkdownSeparator of
Line:
begin
FixString := '-';
SeparatorString := '-|-';
end;
Row:
begin
FixString := '`';
SeparatorString := '` | `';
end;
end;
Writeln(Format('%s%s%s%s%s%s%s%s%s', [
FixString,
aName, SeparatorString,
aType, SeparatorString,
aQualifiedType, SeparatorString,
aKind,
FixString
]));
end;
procedure Main;
var
RttiContext: TRttiContext;
InstanceRttiType: TRttiType;
RttiProperty : TRttiProperty;
PropertyType: TRttiType;
begin
RttiContext := TRttiContext.Create();
MarkdownDump(
Row,
'Name',
'Type.Name',
'Type.QualifiedName',
'Type.TypeKind'
);
MarkdownDump(Line, '-', '-', '-', '-');
try
InstanceRttiType := RttiContext.GetType(TInstance.ClassInfo);
for RttiProperty in InstanceRttiType.GetProperties() do
begin
PropertyType := RttiProperty.PropertyType;
MarkdownDump(
Row,
RttiProperty.Name,
PropertyType.Name,
PropertyType.QualifiedName,
TRttiEnumerationType.GetName<TTypeKind>(PropertyType.TypeKind)
);
end;
finally
RttiContext.Free();
end;
end;
begin
try
Main();
except
on E: Exception do
Writeln(E.ClassName, ': ', E.Message);
end;
end.
Name Type.Name Type.QualifiedName Type.TypeKind
IntegerProperty Integer System.Integer tkInteger
ColorProperty TColor System.UITypes.TColor tkInteger
DoubleProperty Double System.Double tkFloat
DateTimeProperty TDateTime System.TDateTime tkFloat
DateProperty TDate System.TDate tkFloat
TimeProperty TTime System.TTime tkFloat
StringProperty string System.string tkUString
FontNameProperty TFontName System.UITypes.TFontName tkUString
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment