How can I get the element type of an array using RTTI

I use this code to get the type of an array element

{$APPTYPE CONSOLE} uses Rtti, SysUtils; type TFooArray= array of TDateTime; Var T : TRttiType; begin try T:=TRttiContext.Create.GetType(TypeInfo(TFooArray)); Writeln(TRttiArrayType(T).ElementType.Name); except on E: Exception do Writeln(E.ClassName, ': ', E.Message); end; end. 

but the application does not work with access violation in this line

 Writeln(TRttiArrayType(T).ElementType.Name); 

How can I get the element type of an array using RTTI?

+4
source share
1 answer

You incorrectly selected TRttiArrayType for static arrays (and your array is dynamic), to fix the problem, use TRttiDynamicArrayType instead:

  Writeln(TRttiDynamicArrayType(T).ElementType.Name); 
+9
source

Source: https://habr.com/ru/post/1388692/


All Articles