var
rtti: TRttiContext;
typ: TRttiType;
prop1: TRttiInstanceProperty;
prop2: TRttiInstanceProperty;
attr: ^TAttrData;
protOld: Cardinal;
attrObj: TCustomAttribute;
begin
ShowMessage(ClassName + '
: Begin');
rtti := TRttiContext.Create;
typ :=
rtti.GetType(
{ClassType}TTest);
prop1 := typ.GetProperty(
{APropName}'
Test1')
as TRttiInstanceProperty;
prop2 := typ.GetProperty(
{APropName}'
Test2')
as TRttiInstanceProperty;
//for attrObj in prop1.GetAttributes do
// if attrObj is TTest1Attribute then
// ShowMessage(attrObj.ClassName + ': ' + BoolToStr(TTest1Attribute(attrObj).Value, True)) // billig gecastet ... den Value-Property hätte man ja auch via RTTI suchen und auslesen können
// else if attrObj is TTest2Attribute then
// ShowMessage(attrObj.ClassName + ': ' + BoolToStr(TTest2Attribute(attrObj).Value, True))
// else
// ShowMessage(attrObj.ClassName);
attrObj := prop1.GetAttributes[0]; ShowMessage(attrObj.ClassName + '
: ' + BoolToStr((attrObj
as TTest1Attribute).Value, True));
attrObj := prop2.GetAttributes[0]; ShowMessage(attrObj.ClassName + '
: ' + BoolToStr((attrObj
as TTest2Attribute).Value, True));
attr := @GetTypeData(prop1.Handle).AttrData;
// @prop.TypeData^.AttrData; // TypeData ist Privat
VirtualProtect(attr, 4096, PAGE_READWRITE, protOld);
Inc(attr, SizeOf(Word));
// TAttrData.Len überspringen
if PAttrEntry(attr).AttrCtor =
nil then ;
// k.A.
VirtualProtect(attr, 4096, protOld, protOld);
(prop1.GetAttributes[0]
as TTest1Attribute).Value := True;
TTest2Attribute.SetValue('
2', True);
attrObj := prop1.GetAttributes[0]; ShowMessage(attrObj.ClassName + '
: ' + BoolToStr((attrObj
as TTest1Attribute).Value, True));
attrObj := prop2.GetAttributes[0]; ShowMessage(attrObj.ClassName + '
: ' + BoolToStr((attrObj
as TTest2Attribute).Value, True));
// nochmal
rtti := TRttiContext.Create;
typ :=
rtti.GetType(
{ClassType}TTest);
prop1 := typ.GetProperty(
{APropName}'
Test1')
as TRttiInstanceProperty;
prop2 := typ.GetProperty(
{APropName}'
Test2')
as TRttiInstanceProperty;
attrObj := prop1.GetAttributes[0]; ShowMessage(attrObj.ClassName + '
: ' + BoolToStr((attrObj
as TTest1Attribute).Value, True));
attrObj := prop2.GetAttributes[0]; ShowMessage(attrObj.ClassName + '
: ' + BoolToStr((attrObj
as TTest2Attribute).Value, True));
ShowMessage(ClassName + '
: End');