Skip to content

Instantly share code, notes, and snippets.

@AlbertoMonteiro
Created July 16, 2014 19:26
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 AlbertoMonteiro/59165c2abdc3472e9dab to your computer and use it in GitHub Desktop.
Save AlbertoMonteiro/59165c2abdc3472e9dab to your computer and use it in GitHub Desktop.
Usando RTTI para obter Enumerator e a Descricação do Enumerator de forma generica
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, TypInfo, StdCtrls;
type
TEnum1 = (vEnum1, vEnum2);
TEnum2 = (v2Enum1, v2Enum2);
TForm1 = class(TForm)
Button1: TButton;
Edit1: TEdit;
procedure Button1Click(Sender: TObject);
public
function GetNomeCampoAmigavel(eType: PTypeInfo; codigo: string; descArray: array of string): string;
function GetNomeCampo(eType: PTypeInfo; codigo: integer): string;
function MyValString(pMyVal: array of string):string;
end;
const
sTEnum1 : array[TEnum1] of string = ('Valor a', 'Valor B');
sTEnum2 : array[TEnum2] of string = ('Valor enum 2 A', 'Valor enum 2 B');
var
Form1: TForm1;
implementation
{$R *.dfm}
{ TForm1 }
function TForm1.GetNomeCampo(eType: PTypeInfo; codigo: integer): string;
begin
Result := GetEnumName(eType, codigo);
end;
function TForm1.GetNomeCampoAmigavel(eType: PTypeInfo; codigo: string; descArray: array of string): string;
var
OrdTypeInfo: PTypeInfo;
OrdTypeData: PTypeData;
TypeNameStr: string;
TypeKindStr: string;
ix, MinVal, MaxVal: Integer;
begin
OrdTypeData := GetTypeData(eType);
{- Get the type kind string }
MinVal := OrdTypeData^.MinValue;
MaxVal := OrdTypeData^.MaxValue;
for ix := MinVal to MaxVal do
begin
if GetEnumName(eType, ix) = codigo then
begin
ShowMessage(descArray[ix]);
Exit;
end
end
end;
procedure TForm1.Button1Click(Sender: TObject);
var
campo : string;
begin
campo := GetNomeCampo(TypeInfo(TEnum2), StrToInt(Edit1.Text));
ShowMessage(campo);
GetNomeCampoAmigavel(TypeInfo(TEnum2),campo,sTEnum2);
end;
end.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment