1

I want to enumerate field information (name, type, …) of a record. RTTI delivers field name but type is null(nil)! How can I get this information?

Record:

 foo = record
  bar : array[0..5] of char;
 end;

Enumeration:

  for var f : TRttiField  in TRTTIContext.Create.GetType(TypeInfo(foo)).GetFields do
  begin
    OutputDebugString(PWideChar(f.Name + ' :: ' + f.FieldType.ToString())); ///fieldtype is nil??!
  end;

2 Answers 2

3

The RTTI system only works with predefined types. Defining field types "on-the-fly" does not generate RTTI information. So, declare the array type like this instead:

type
  TChar5Arr = array[0..5] of Char;

  foo = record
    bar : TChar5Arr;
  end;

And you will get some more info:

name: bar
type: TChar5Arr
value: (array)    //is not retrieved using GetValue
Sign up to request clarification or add additional context in comments.

Comments

0

Per Delphi's documentation:

Querying for Type Information

In Delphi, type information is emitted only for types and not for global unit routines, constants, or variables. This means that the obtained TRttiContext can be used only to query for information about the declared types.

1 Comment

I feel that this snippet lacks context. What does "this" refer to? Did you intend to emphasise "declared"? What's the conclusion? You may want to add a few words to this answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.