变体记录的 Rtti
我尝试用 Delphi 2010 编写一种对象/记录序列化器,想知道是否有办法检测记录是否是变体记录。例如,Types.pas 中定义的 TRect 记录:
TRect = record
case Integer of
0: (Left, Top, Right, Bottom: Longint);
1: (TopLeft, BottomRight: TPoint);
end;
由于我的序列化程序应该在我的数据结构上递归工作,因此它将下降到 TPoint 记录并在我的序列化文件中生成冗余信息。有没有办法通过获取记录中的详细信息来避免这种情况?
I try to write a kind of object/record serializer with Delphi 2010 and wonder if there is a way to detect, if a record is a variant record. E.g. the TRect record as defined in Types.pas:
TRect = record
case Integer of
0: (Left, Top, Right, Bottom: Longint);
1: (TopLeft, BottomRight: TPoint);
end;
As my serializer should work recursively on my data structures, it will descent on the TPoint records and generate redundant information in my serialized file. Is there a way to avoid this, by getting detailed information on the record?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
一种解决方案可能如下:
但该解决方案并不是适合所有情况的正确解决方案。它仅适用于序列化,如果不同的变体包含相同的信息和相同的类型。如果您有类似以下内容(来自 wikipedia.org):
您会序列化
还是序列化会更好 是
的,当然,这对于计算机来说也是相同的,但对于应该可读甚至可编辑的文件来说则不同对于人类来说。
所以我认为,解决问题的唯一方法是使用属性来定义应该使用哪个变体进行序列化。
One solution could be as follows:
But this solution is not a proper solution for all cases. It only works for serialization, if the different variants contain the same information and the same types. If you have something like the following (from wikipedia.org):
Would you serialize
or would it be better to serialize
Yes, for sure, this would also be the same for a computer but not for a file which should be readable or even editable for humans.
So I think, the only way to solve the problem is using an Attribute, to define, which variant should be used for serialization.