尝试访问类属性时出错

时间:2012-02-29 12:24:31

标签: 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;

我这样说:TReportItem.GetTableName<TReportItem>; <>可以是继承TReportItem的任何类。

但是,有时当我在命令TReportItem.GetTableName中调用LRttiType.GetAttributes时,我会获得访问冲突,有时不会,取决于“编译”。它起作用并停止像魔法一样工作。 我不知道发生了什么。有人可以给我一个暗示吗?

问题出现在GetAttributes,如果我用它来获取fiels,方法等属性。它会让我违反访问权限。是否有一些指令我必须打开或关闭才能使用它?

如果我使用 Shift + F9 编译,GetAttributes给我AV,如果我修改单位中的任何一行并使用 F9 {{1工作。

不仅在我的机器上,其他8位程序员也遇到了同样的问题。德尔福XE。

rtti.pas 中的此代码中出现错误:

GetAttributes

1 个答案:

答案 0 :(得分:0)

今天几个小时我遇到了完全相同的问题,Rtti.pas中的imp := PPPointer(p)^^上有相同的AV。

我发现我在项目的2个不相关的单元中使用了2个属性的相同名称:在我重命名其中一个之后,不再有AV!

相关问题