Tôi có lớp này:Lỗi khi cố gắng truy cập vào các thuộc tính lớp
{$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;
tôi gọi nó theo cách này: TReportItem.GetTableName<TReportItem>
; <>
có thể là bất kỳ lớp nào kế thừa TReportItem
.
Nhưng, đôi khi tôi gọi: TReportItem.GetTableName
trong lệnh LRttiType.GetAttributes
Tôi nhận được một sự vi phạm truy cập, đôi khi không, phụ thuộc vào 'biên dịch'. Nó hoạt động và ngừng hoạt động như ma thuật. Tôi không biết điều gì đang xảy ra. Ai đó có thể cho tôi một gợi ý?
Sự cố xảy ra ở số GetAttributes
, nếu tôi sử dụng điều đó để lấy thuộc tính trong fiels, phương pháp, v.v. Nó cho tôi vi phạm quyền truy cập. Có một số chỉ thị mà tôi phải bật hoặc tắt để sử dụng nó?
Nếu tôi biên dịch sử dụng Shift + F9, các GetAttributes
cho tôi AV, nếu tôi sửa đổi bất kỳ dòng trong đơn vị và biên dịch sử dụng F9GetAttributes
công trình.
Nó không chỉ có trong máy của tôi, 8 lập trình viên khác có cùng vấn đề. Delphi XE.
Lỗi này xảy ra trong mã này trong 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;
Bạn có thể hiển thị cuộc gọi của mình tới 'TReportItem.GetTableName' hay không. –
Khi bạn nhận được AV, giá trị 'LRttiType' có giá trị gì? –
Xin lỗi, AV là gì? –