Name

TypeInfo Function

Syntax

function TypeInfo(Type name): Pointer;

Description

The TypeInfo function returns a pointer to a type’s runtime type information. The pointer is actually a PTypeInfo pointer, which is a pointer to a TTypeInfo record. These types are declared in the TypInfo unit.

See Chapter 3 for more information about runtime type information.

Example

// Convert a set to a string representation, e.g.,
// '[fsBold,fsItalic]'.
function SetToString(Info: PTypeInfo; const Value): string;
var
  I: Integer;
  Data: PTypeData;        // set's type data
  EnumInfo: PTypeInfo;	 // set's base type info
  EnumData: PTypeData;    // set's base type data
begin
  if Info.Kind <> tkSet then
    Result := ''
  else
  begin
    Data := GetTypeData(Info);
    EnumInfo := Data^.CompType^;
    EnumData := GetTypeData(EnumInfo);

    Assert(EnumInfo.Kind in [tkEnumeration,tkInteger]);

    Result := '[';
    for I := EnumData.MinValue to EnumData.MaxValue do
      if I in TIntegerSet(Value) then
      begin
        // The element is in the set, so add its name to the string.
        if Length(Result) > 1 then
          Result := Result + ',';  // Separate items with commas.
        Result := Result + GetEnumName(EnumInfo, I);
      end;
    Result := Result + ']';
  end;
end;
...
S := SetToString(TypeInfo(TFontStyles), Control.Font.Style);

Get Delphi in a Nutshell now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.