How can I test if an unknown Delphi RTTI TValue reflects an object that is ANY type of generic TList<> (or at least TEnumerable<>)?

后端 未结 1 1048
孤城傲影
孤城傲影 2021-01-14 10:56

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

相关标签:
1条回答
  • 2021-01-14 11:28

    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.

    1. use TRttiType.Name to get the class name as a string ('TList<System.string>').

    2. parse it to detect the presence of angle brackets ('<>').

    3. extract the substring between the brackets ('System.string')

    4. 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.

    0 讨论(0)
提交回复
热议问题