UNIT Unit_1;
Interface
uses
System.Rtti;
type
FuncInfoAttribute =
class(TCustomAttribute)
private
FGL:
string;
Fn_Var: Integer;
public
constructor Create(AGL:
string; An_Var: Integer);
property GL:
string read FGL;
property n_Var: Integer
read Fn_Var;
end;
type
TRttiHelper =
record
public
class function FindAttribute<T: TCustomAttribute>(Source: TClass): T;
overload;
static;
class function FindAttribute<T: TCustomAttribute>(Source: TRttiObject): T;
overload;
static;
end;
//------------------------------------------------------------------------------------
Type
[FuncInfo('
Funktion mit ZWEI Variablen', 2)]
TFunk_2 =
Class(TObject)
Strict Private
function GetGL:
String;
Function Get_n_var: Integer;
Public
property GL:
String read GetGL;
Property n_Var: Integer
Read Get_n_var;
End;
//------------------------------------------------------------------------------------
Type
[FuncInfo('
Funktion mit DREI Variablen', 3)]
TFunk_3 =
Class(TFunk_2)
End;
//------------------------------------------------------------------------------------
Implementation
function TFunk_2.GetGL:
String;
var
attr: FuncInfoAttribute;
begin
attr := TRttiHelper.FindAttribute<FuncInfoAttribute>(ClassType);
Result := attr.GL;
end;
Function TFunk_2.Get_n_var: Integer;
var
attr: FuncInfoAttribute;
begin
attr := TRttiHelper.FindAttribute<FuncInfoAttribute>(ClassType);
Result := attr.n_Var;
End;
constructor FuncInfoAttribute.Create(AGL:
string; An_Var: Integer);
begin
FGL := AGL;
Fn_Var := An_Var;
end;
class function TRttiHelper.FindAttribute<T>(Source: TClass): T;
var
context: TRttiContext;
myType: TRttiType;
begin
Result :=
nil;
context := TRttiContext.Create;
myType := context.GetType(Source);
if myType <>
nil then begin
Result := FindAttribute<T>(myType);
end;
end;
class function TRttiHelper.FindAttribute<T>(Source: TRttiObject): T;
var
attr: TCustomAttribute;
attributes: TArray<TCustomAttribute>;
begin
Result :=
nil;
attributes := Source.GetAttributes;
for attr
in attributes
do begin
if attr
is T
then begin
Result := T(attr);
Break;
end;
end;
end;
Initialization
TRttiContext.KeepContext;
Finalization
TRttiContext.DropContext;
End.