Rod*_*ino 6 delphi attributes rtti
我有这门课:
{$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;
Run Code Online (Sandbox Code Playgroud)
我把这种方式:TReportItem.GetTableName<TReportItem>; 该<>可以继承任何类TReportItem.
但是,有时我打电话:TReportItem.GetTableName在命令中LRttiType.GetAttributes我得到访问冲突,有时不是,取决于'编译'.它起作用并停止像魔法一样工作.我不知道发生了什么.有人可以给我一个提示吗?
问题在于GetAttributes,如果我使用它来获取fiels,方法等属性.它会给我访问冲突.是否有一些指令我必须打开或关闭才能使用它?
如果我编译使用Shift+F9,GetAttributes给我AV,如果我修改单位中的任何行,并使用F9 GetAttributes作品编译.
它不仅在我的机器上,其他8个程序员也有同样的问题.德尔福XE.
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;
Run Code Online (Sandbox Code Playgroud)
小智 0
今天我在 Rtti.pas 中遇到了完全相同的问题几个小时,同样的 AV imp := PPPointer(p)^^。
我发现我在项目中的 2 个不相关单元中对 2 个属性使用了相同的名称:在我重命名其中一个后,不再有 AV!
| 归档时间: |
|
| 查看次数: |
767 次 |
| 最近记录: |