List all the properties from a class that implements an interface

后端 未结 1 436
面向向阳花
面向向阳花 2021-01-21 10:15

I have a class that has an interface on it:

TInterface = interface(IXMLNode)
  function Get_One: Boolean;
  function Get_Two: Boolean;
  function Get_Three: Bool         


        
相关标签:
1条回答
  • 2021-01-21 10:42

    GetPropList() is based on old-style RTTI that only describes class properties and class methods which are declared as published (which none of your items are) and only if the class, or an ancestor (like TPeristent), has been marked as {$M+}.

    Since you are using XE2, you can use Extended RTTI (which was introduced in Delphi 2010) instead. It does not have such limitations. For example:

    uses
      System.Rtti;
    
    var
      Ctx: TRttiContext;
      PropList: TArray<TRttiProperty>;
    begin
      PropList := Ctx.GetType(TTesting).GetProperties;
      ...
    end;
    

    Update: interfaces are a special case. An interface is only allowed to contain abstract methods, properties are just syntax sugar to call those methods. So properties defined on an interface are not real properties, like they are on class types, and thus do not generate RTTI. That is why you cannot enumerate properties that are inherited from an interface. You can enumerate the interface's methods using Extended RTTI, but only if the interface has been marked with {$M+}.

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