2

RTTiでいくつか問題が発生しました..レコードタイプのすべての定数値を列挙したいです

 type TMyRecord = record
  const
    value1: Integer=10;
    value2: Integer=13;
    value3: Integer=18;
    value4: Integer=22;
 end;
procedure TForm3.Button1Click(Sender: TObject);
var
 ctx:TRttiContext ;
 Field:rtti.TRttiField       ;
begin
 for Field in ctx.GetType(TypeInfo(TMyRecord)).GetFields     do
 ListBox1.Items.Add(Field.Name  );  // i got nothing
end;

しかし、私のレコードが定数でない場合、私のコードは正常に機能します

 type TMyRecord = record
   value1: Integer;
   value2: Integer;
   value3: Integer;
   value4: Integer;
  end;
procedure TForm3.Button1Click(Sender: TObject);
var
 ctx:TRttiContext ;
 Field:rtti.TRttiField       ;
begin
 for Field in ctx.GetType(TypeInfo(TMyRecord)).GetFields     do
 ListBox1.Items.Add(Field.Name  );  //its work
end;
4

1 に答える 1

4

RTTIは定数を列挙できません。それらはフィールドのように見えるかもしれませんが、そうではありません。これらは、他の定数と同じように、レコードの名前空間内に実装されます。

別のアプローチを検討する必要があるかもしれません。たとえば、定数の代わりに属性を使用できます。または、これらの定数を列挙するクラス関数を追加することもできます。

さらに別のアプローチは次のようになります。

type
  TMyRecord = record
    value1: Integer;
    value2: Integer;
    value3: Integer;
    value4: Integer;  
 end;

const
  MyConst: TMyRecord = (
    value1: 10;
    value2: 13;
    value3: 18;
    value4: 22
  );
于 2012-10-27T08:12:14.297 に答える