Difference between revisions of "Runtime Type Information (RTTI)"

From Free Pascal wiki
Jump to navigationJump to search
(→‎Converting a enumerated type to a string: move uses clause to the top.)
Line 7: Line 7:
  
 
<syntaxhighlight>
 
<syntaxhighlight>
 +
uses TypInfo;
 +
 
type
 
type
 
   TProgrammerType = (tpDelphi, tpVisualC, tpVB, tpJava) ;
 
   TProgrammerType = (tpDelphi, tpVisualC, tpVB, tpJava) ;
 
uses TypInfo;
 
  
 
var  
 
var  
Line 17: Line 17:
 
   s := GetEnumName(TypeInfo(TProgrammerType), integer(tpDelphi));
 
   s := GetEnumName(TypeInfo(TProgrammerType), integer(tpDelphi));
 
   // Here s = 'tpDelphi'
 
   // Here s = 'tpDelphi'
 +
  Writeln(s)
 +
end.
 
</syntaxhighlight>
 
</syntaxhighlight>
  

Revision as of 10:20, 17 July 2016

English (en) français (fr) русский (ru)


RTTI can be utilized to obtain a number of meta information in a Pascal application.

Converting a enumerated type to a string

One can use RTTI to obtain a string from a enumerated type.

uses TypInfo; 

type
  TProgrammerType = (tpDelphi, tpVisualC, tpVB, tpJava) ;

var 
  s: string;
begin
  s := GetEnumName(TypeInfo(TProgrammerType), integer(tpDelphi));
  // Here s = 'tpDelphi'
  Writeln(s)
end.

See Also