I'm trying to iterate over a Dynamic array passed into a generic function
I'm using TValue to achive this, but i can't get the length of the array and there for I can not get the elements.
I've written a small demo project to illustrate my problem:
program Project1;
{$APPTYPE CONSOLE}
{$R *.res}
uses
System.SysUtils, System.TypInfo, System.Rtti;
type
TMyEnum = (me_One, me_Two, me_Three);
Writer = class
public
class procedure Write<T>(Value: T);
end;
{ Writer }
class procedure Writer.Write<T>(Value: T);
var
ArrayValue: TValue;
TypeInfo: pTypeInfo;
begin
TypeInfo := System.TypeInfo(T);
if TypeInfo.Kind <> tkDynArray then
exit;
TValue.Make(nil, TypeInfo, ArrayValue);
Writeln(ArrayValue.GetArrayLength);
//Here I have my problem ArrayValue.GetArrayLength returns 0 and not 2
end;
var
Enums: array of TMyEnum;
Dummy : String;
begin
try
SetLength(Enums, 2);
Enums[0] := me_One;
Enums[1] := me_Two;
Writer.Write(Enums);
Readln(Dummy);
except
on E: Exception do
Writeln(E.ClassName, ': ', E.Message);
end;
end.
niltoTValue.Makeand not@Value?