以下之间的语义差异是什么:
RttiType.TypeKind 和 RttiType.Name ?
我问是因为原则上不能从名称推断出 TypeKind 吗?
最佳答案
TRttiType
的 TypeKind
和 Name
属性是完全不同的东西。
TypeKind
告诉您您拥有哪种类型。这可以是TTypeKind
枚举类型中定义的 23 个不同选项之一。Name
告诉您您的类型。这是一个字符串,有无限数量的可能值。
不同的类型(通常)具有不同的名称,但可能具有相同的TypeKind
。例如,考虑这个简单的演示。
program RttiDemo;
{$APPTYPE CONSOLE}
uses
Rtti;
procedure Main;
var
Context: TRttiContext;
TObjectType, TInterfacedObjectType: TRttiType;
begin
TObjectType := Context.GetType(TObject);
TInterfacedObjectType := Context.GetType(TInterfacedObject);
Writeln(TObjectType.Name);
Writeln(TInterfacedObjectType.Name);
Assert(TObjectType.TypeKind=TInterfacedObjectType.TypeKind);
end;
begin
Main;
Readln;
end.
输出为:
TObject TInterfacedObject
因此,您无法从类型 name 推断出类型 kind,因为 kind 和 name 是完全不同的东西。
关于delphi - RttiType.TypeKind 和 RttiType.Name 之间有什么区别?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16907411/