In Delphi, if I have a TValue
instance reflecting an unknown object, how can I test if this object is an instance of ANY kind of generic TEnumerable<&g
There is no RTTI generated for Generic types themselves (they don't exist at runtime), and each specific instantiation (like TList<string>
) is a distinct class type with its own distinct RTTI. You would have to check for each individual type, it is not possible to test for any Generic type. Parsing class names is the only way to detect Generic types.
use TRttiType.Name
to get the class name as a string ('TList<System.string>'
).
parse it to detect the presence of angle brackets ('<>'
).
extract the substring between the brackets ('System.string'
)
walk the ancestor tree looking for an ancestor whose TRttiType.Name
is 'TEnumerable<...>'
, where ...
is the extracted substring ('TEnumerable<System.string>'
).
However, this approach fails for class types that derive from TEnumerable<T>
but do not have Generics parameters themselves, eg:
type
TMyClass = class(TEnumerable<string>)
end;
To account for that, ignore steps 1-3 and jump right to step 4 by itself, ignoring whatever value appears between the brackets, eg:
function IsAnyKindOfGenericEnumerable(AType: TRttiType): Boolean;
begin
Result := False;
while AType <> nil do
begin
Result := StartsText('TEnumerable<', AType.Name);
if Result then Exit;
AType := AType.BaseType;
end;
end;
As for TRttiEnumerationType
, it represents enumerated types (ie: type typeName = (val1, ...,valn);
). It has nothing to do with TEnumerable<T>
. That is why the is
operator is always returning False for you - none of the RTTI types you are testing represent enums.