获取静态数组的RTTI记录字段类型失败 [英] Getting type of record field with RTTI fails for static arrays

查看:220
本文介绍了获取静态数组的RTTI记录字段类型失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试获取记录字段的类型,以便创建正确的比较器(作为任何/几乎任何记录类型的通用解决方案)。我找不到静态数组的类型信息:

I am trying to get types for record fields in order to create correct comparer (as general solution for any/almost any record type). I can't find type information for static arrays:

  TArrFieldTest = record
    a: string;
    b: array[0..3] of byte;
  end;

procedure Test;
var
  rttiContext: TRttiContext;
  rttiType: TRttiType;
  rttiFields: TArray<TRttiField>;
begin
  rttiType := rttiContext.GetType(TypeInfo(TArrFieldTest));
  rttiFields := rttiType.GetFields;
  Assert(rttiFields[0].FieldType<>nil); // it's ok
  Assert(rttiFields[1].FieldType<>nil); // fail here!
end;

FieldType对于任何类型的静态数组都是零。
任何想法这里有什么问题?
或者也许有更简单的方法来创建与TArray / TDictionary等一起使用的记录的比较器?

FieldType is nil for static array of any type. Any ideas what is wrong here? Or maybe there is easier way to create comparer for records to be used with TArray/TDictionary etc?

推荐答案

您需要声明一个类型才能使RT​​TI可用。例如:

You need to declare a type in order to have RTTI available. For example:

type
  TMyStaticArrayOfByte = array[0..3] of byte;

  TArrFieldTest = record
    a: string;
    b: TMyStaticArrayOfByte;
  end;

这篇关于获取静态数组的RTTI记录字段类型失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆