I have this class:
{$RTTI EXPLICIT FIELDS([vcProtected]) PROPERTIES([vcProtected])}
const
PP_VEHICLE_FIELD = 'VEICULO_ID';
PP_DRIVER_FIELD = 'MOTORISTA_ID';
PP_TRIP_FIELD = 'VIAGEM_ID';
PP_DATE = 'DATA';
type
[TAttrDBTable('NONE')]
TReportItem = class(TObject)
protected
[TAttrDBField(PP_VEHICLE_FIELD)]
FVeiculoId: integer;
[TAttrDBField(PP_DRIVER_FIELD)]
FMotoristaId: integer;
[TAttrDBField(PP_TRIP_FIELD)]
FViagemId: integer;
[TAttrDBField(PP_DATE)]
FDataRelatorio: TDate;
published
class function GetTableName<T: class, constructor>: string;
end.
class function TReportItem.GetTableName<T>: string;
var
LRttiContext: TRttiContext;
LRttiType: TRttiType;
LCustomAttribute: TCustomAttribute;
LType: T;
begin
LType := T.Create;
try
LRttiContext := TRttiContext.Create;
LRttiType := LRttiContext.GetType(LType.ClassType);
for LCustomAttribute in LRttiType.GetAttributes do
if LCustomAttribute is TAttrDBTable then
begin
Result := TAttrDBTable(LCustomAttribute).TableName;
Break;
end;
finally
LType.Free;
end;
end;
I call it this way: TReportItem.GetTableName<TReportItem>
; The <>
can be any class that inherit TReportItem
.
But, sometimes when I call: TReportItem.GetTableName
in the command LRttiType.GetAttributes
I get an access violation, sometimes not, depends of the 'compilation'. It works and stop working like magic.
I don't know what is happening. Someone can give me a hint ?
The problem is on the GetAttributes
, if I use that to get attributes in fiels, methods etc. It give me access violation. Is there some directive that I must to turn on or off to use it?
If I compile using Shift+F9, the GetAttributes
give me AV, if I modify any line in the unit and compile using F9 GetAttributes
works.
It's not only in my machine, other 8 programmers are with the same problem. Delphi XE.
The error occurs in this code in rtti.pas:
function FindCtor(AttrType: TRttiInstanceType; CtorAddr: Pointer): TRttiMethod;
type
PPPointer = ^PPointer;
var
p: PByte;
imp: Pointer;
begin
for Result in AttrType.GetMethods do
if Result.CodeAddress = CtorAddr then
Exit;
// expect a package (i.e. DLL) import
p := CtorAddr;
Assert(p^ = $FF); // $FF $25 => indirect jump m32
Inc(p);
Assert(p^ = $25);
Inc(p);
imp := PPPointer(p)^^; //ERROR HAPPENS HERE
for Result in attrType.GetMethods do
if Result.CodeAddress = imp then
Exit;
Result := nil;
end;
TReportItem.GetTableName
. – JoloLRttiType
have? – JologetAttributes
is used only to get class attributes. (if your fields and props are public, then seems to me, you have to inlclude vcPublic in$RTTI
directive too) – PeruT
is supposed to be a class that descends fromTReportItem
, then just callT.GetTableName
. Within the function, replaceT
withSelf
. You can also replaceLType.ClassType
withSelf
. DeclareLType
asTReportItem
. – Farl