Delphi RTTI-为什么在某些情况下TTypedData.CompType为nil?

Delphi RTTI-为什么在某些情况下TTypedData.CompType为nil?,delphi,delphi-xe2,Delphi,Delphi Xe2,我有一个t值包含一个集合类型数据。CompType为零。因此调用TValue.ToString会引发异常,因为System.typenfo.SetToString假定CompType永远不会为零 为什么对于某些集合类型,CompTypenil TTestEnumType = (tstEnum1 = 1, tstEnum2 = 2, tstEnum3 = 3, tstEnum4 = 4, tstEnum5 = 5, tstEnum6 = 6, tstEnum7 = 7); TTestEnumTyp

我有一个
t值
包含一个集合<代码>类型数据。CompType为零。因此调用
TValue.ToString
会引发异常,因为
System.typenfo.SetToString
假定
CompType
永远不会为零

为什么对于某些集合类型,
CompType
nil

TTestEnumType = (tstEnum1 = 1, tstEnum2 = 2, tstEnum3 = 3, tstEnum4 = 4, tstEnum5 = 5, tstEnum6 = 6, tstEnum7 = 7);
TTestEnumTypeSet = set of TTestEnumType;
TTestSetOfByte = set of Byte;
在上面,我们定义了两种集合类型:
TTestEnumTypeSet
TTestSetOfByte

下面的简单测试表明,对于
TTestesteToByte
,CompType为零

procedure TTestUtlRttiComparer.TestSetToString;
var
  TypeData1: TTypeData;
  TypeData2: TTypeData;
  TypeInfo1: TTypeInfo;
  TypeInfo2: TTypeInfo;
begin
  TypeInfo1 := PTypeInfo(TypeInfo(TTestSetOfByte))^;
  TypeInfo2 := PTypeInfo(TypeInfo(TTestEnumTypeSet))^;
  CheckTrue(TypeInfo1.Kind = tkSet);
  CheckTrue(TypeInfo2.Kind = tkSet);

  TypeData1 := GetTypeData(@TypeInfo1)^;
  TypeData2 := GetTypeData(@TypeInfo2)^;
  CheckTrue(Assigned(TypeData1.CompType));
  CheckTrue(Assigned(TypeData2.CompType), 'TypeData2.CompType is NULL!!!! WHY??????'); // this FAILS!!!
end;

具有显式分配的序号的枚举类型没有RTTI。这在以下文件中说明:

没有特定值的枚举常量具有RTTI:

type SomeEnum = (e1, e2, e3);
type SomeEnum = (e1 = 1, e2 = 2, e3 = 3);
而具有特定值的枚举常量,例如 以下是没有RTTI的情况:

type SomeEnum = (e1, e2, e3);
type SomeEnum = (e1 = 1, e2 = 2, e3 = 3);

哇,太快了!还需要8分钟,所以我甚至可以接受你的答案在这种情况下,{$M+}是否仍然可以强制RTTI。模糊记忆已经有一段时间没有完成Delphi了…@TonyHopkinson不,这些类型根本没有RTTI。@DavidHeffernan现在我遇到了一个showstopper。问题是,这些具有指定顺序的枚举已经存在多年了。因此,修改它们将是一项巨大的任务(尤其是测试)。你能想出任何方法从引用这样一个枚举的TrtIfield或TrtIproperty中获取TValue吗?如果没有任何RTTI,你基本上被卡住了。你无能为力。