You need a special attribute class:
type
CodeIntAttribute = class(TCustomAttribute)
private
FValue: Integer;
public
constructor Create(AValue: Integer);
property Value: Integer read FValue;
end;
....
constructor CodeIntAttribute.Create(AValue: Integer);
begin
inherited Create;
FValue := AValue;
end;
I decided to make the value an integer that seems more appropriate than a string.
Then you define the properties as follows:
[CodeInt(2800)]
property UtilisateurCode: string read FUtilisateurCode write FUtilisateurCode;
[CodeInt(2801)]
property UtilisateurCle: string read FUtilisateurCle write FUtilisateurCle;
Finally, the implementation SetCodeInt:
procedure TUserClass.SetCodeInt(ACode: Integer; AValue: string);
var
ctx: TRttiContext;
typ: TRttiType;
prop: TRttiProperty;
attr: TCustomAttribute;
codeattr: CodeIntAttribute;
begin
typ := ctx.GetType(ClassType);
for prop in typ.GetProperties do
for attr in prop.GetAttributes do
if attr is CodeIntAttribute then
if CodeIntAttribute(attr).Value=ACode then
begin
prop.SetValue(Self, TValue.From(AValue));
exit;
end;
raise Exception.CreateFmt('Property with code %d not found.', [ACode]);
end;
source
share