【发布时间】:2015-03-16 22:03:27
【问题描述】:
我为 Byte 集合定义了一个类型、一个接口和一个实现该接口的类。 该接口具有 TTestSetofByte + getter 和 setter 类型的属性。没什么特别的。
type
TTestSetOfByte = set of Byte;
ITestInterface = interface
['{BCF0CEC2-F999-4E8A-A732-416F343C1629}']
function GetPropSetOfByte: TTestSetOfByte;
procedure SetPropSetOfByte(const Value: TTestSetOfByte);
property PropSetOfByte: TTestSetOfByte read GetPropSetOfByte write SetPropSetOfByte;
end;
TTestClass3 = class(TInterfacedObject, ITestInterface)
private
FSetOfByte: TTestSetOfByte;
function GetPropSetOfByte: TTestSetOfByte;
procedure SetPropSetOfByte(const Value: TTestSetOfByte);
public
constructor Create;
property PropSetOfByte: TTestSetOfByte read GetPropSetOfByte write SetPropSetOfByte;
end;
问题是,当我尝试读取 PropSetOfByte 属性的值时,delphi 会抛出 EAccessViolation,我不明白为什么。其他类型(int, string) 的属性工作得很好。
这里是测试代码:
procedure TTestUtlRttiComparer.DeleteMe;
var
i: Integer;
Instance1: ITestInterface;
Object1: TObject;
RttiContext: TRttiContext;
RttiProp: TRttiProperty;
RttiValue1: TValue;
Type1: TRttiType;
begin
Instance1 := TTestClass3.Create;
Check(Instance1.PropSetOfByte = [1,4], 'Making sure getter works!');
Instance1.PropSetOfByte := [3,4];
Check(Instance1.PropSetOfByte = [3,4], 'Making sure setter works!');
Object1 := (Instance1 as TObject);
Check(Assigned(Object1));
RttiContext := TRttiContext.Create;
try
Type1 := RttiContext.GetType(Object1.ClassInfo);
// Properties pruefen
for i := 0 to High(Type1.GetProperties) do
begin
RttiProp := Type1.GetProperties[i];
if RttiProp.Name = 'PropSetOfByte' then
begin
RttiValue1 := RttiProp.GetValue(Object1); // THIS CHECK FAILS with EACESSVIOLATION!!!
end;
end;
finally
RttiContext.Free;
end;
end;
我正在使用 XE-2。
谢谢!
【问题讨论】:
-
编译器是否为集合类型的属性生成RTTI?
-
@DavidHeffernan:是的,确实如此。
标签: delphi delphi-xe2 rtti