delphi - Create an attribute for an inherited property -
[edited] i'm trying rtti recognise "items" property of tlist generic class can apply attribute it. i've written console app outputs rtti properties here:
program rttiattributes; {$apptype console} uses classes, generics.collections, rtti, sysutils; type tdatamember = class(tcustomattribute); tcustomer = class private fname: string; public [tdatamember] property name: string read fname write fname; // <- attribute recognised rtti end; { inheritance tree: tlist<t> -> tobjectlist<t> -> tcustomerlist here base properties declared in tlist<t> tlist<t> = class(tenumerable<t>) ... public property capacity: integer read getcapacity write setcapacity; property count: integer read fcount write setcount; property items[index: integer]: t read getitem write setitem; default; <- not recognised rtti property...! property onnotify: tcollectionnotifyevent<t> read fonnotify write fonnotify; end; } tcustomerlist = class(tobjectlist<tcustomer>) public //[tdatamember] property items; // <- inherited property, not recognised rtti & hence cannot inspect attributes end; procedure dorttiobject(aobject: tobject); var lcontext: trtticontext; lprop: trttiproperty; lattr: tcustomattribute; begin lcontext := trtticontext.create; try writeln('process class: ' + aobject.classname); lprop in lcontext.gettype(aobject.classtype).getproperties begin writeln('found property: ' + aobject.classname + '.' + lprop.name); lattr in lprop.getattributes if lattr tdatamember writeln(aobject.classname + '.' + lprop.name + ': has tdatamember attribute') else writeln(aobject.classname + '.' + lprop.name + ': not have tdatamember attribute'); end; writeln(''); lcontext.free; end; end; var llist: tcustomerlist; begin llist := tcustomerlist.create; llist.add(tcustomer.create); dorttiobject(llist); dorttiobject(llist[0]); readln; end.
the above console app produces following output:
process class: tcustomerlist found property: tcustomerlist.ownsobjects found property: tcustomerlist.capacity found property: tcustomerlist.count found property: tcustomerlist.onnotify process class: tcustomer found property: tcustomer.name tcustomer.name: has tdatamember attribute
so whatever reason can't determine, rtti recognises 3 not 4 of declared properties of ancestor class.
any ideas?
Comments
Post a Comment