Jump to content
pyscripter

Access class fields and properties with RTTI

Recommended Posts

I can confirm that class variables and properties are not accessible in RS 11.2...

Program ClassProppertyRTTI;

{$APPTYPE CONSOLE}

{$R *.res}


Uses
  System.SysUtils,
  System.Rtti;

Type
  TMyClass = Class
  Strict Private
    FGoodbye: Integer;
  Strict Private
    Class Var FHello: String;
  Strict Protected
  Public
    Class Property Hello: String Read FHello;
    Property Goodbye : Integer Read FGoodbye;
  End;

Var
  C: TMyClass;
  Ctx: TRTTIContext;
  F : TRTTIField;
  T: TRttiType;
  P: TRttiProperty;

Begin
  Try
    C := TMyClass.Create;
    Try
      Ctx := TRTTIContext.Create();
      T := Ctx.GetType(C.ClassInfo);
      For F IN T.GetFields Do
        WriteLn('Field: ', F.Name);
      For P In T.GetProperties Do
        WriteLn('Property: ', P.Name);
    Finally
      C.Free;
    End;
    ReadLn;
  Except
    On E: Exception Do
      WriteLn(E.ClassName, ': ', E.Message);
  End;

End.

It only outputs FGoodbye and Goodbye.

I didn't actually know this restriction in the RTTI.

  • Thanks 1

Share this post


Link to post

In fact, no information is generated for "class var/procedure/function" in RAD 11.2

{$M+}

  TMyClass = class
  private
    FPrivatedField: string;
    procedure FProcedurePrivatedField(const AValue: string);
    class var FClassVarPrivatedField: string;
    class procedure FClassProcedurePrivatedField(const AValue: string); static;
  protected
    FProtectedField: string;
    procedure FProcedureProtectedField(const AValue: string);
    class var FClassVarProtectedField: string;
    class procedure FClassProcedureProtectedField(const AValue: string); static;
  public
    FPublicatedField: string;
    procedure FProcedurePublicField(const AValue: string);
    class var FClassVarPublicField: string;
    class procedure FClassProcedurePublicField(const AValue: string); static;
  published
    FPublishedField: TMyPublished;
    procedure FProcedurePublishedField(const AValue: TMyPublished);
    class var FClassVarPublishedField: TMyPublished;
    class procedure FClassProcedurePublishedField(const AValue: TMyPublished); static;
  public
    class property ClassVarPrivatedField  : string read FClassVarPrivatedField write FClassProcedurePrivatedField;
    class property ClassVarProtectedField : string read FClassVarProtectedField write FClassProcedureProtectedField;
    class property ClassVarPublicatedField: string read FClassVarPublicField write FClassProcedurePublicField;
    class property ClassVarPublishedField : TMyPublished read FClassVarPublishedField write FClassProcedurePublishedField;
    //
    property PrivatedField  : string read FPrivatedField write FProcedurePrivatedField;
    property ProtectedField : string read FProtectedField write FProcedureProtectedField;
    property PublicatedField: string read FPublicatedField write FProcedurePublicField;
    property PublishedField : TMyPublished read FPublishedField write FProcedurePublishedField;
  end;
{$M-}
Quote

Fields:
mvPrivate,FPrivatedField
mvProtected,FProtectedField
mvPublic,FPublicatedField
mvPublished,FPublishedField

Methods:
Method: Contructor,xxxxxxxxxx,xxxxxxxxxxxx,xxxxxx,mvPublic,mkConstructor,dkStatic,-12,Create
Method: xxxxxxxxxx,Destructor,xxxxxxxxxxxx,xxxxxx,mvPublic,mkDestructor,dkVtable,-1,Destroy
Method: xxxxxxxxxx,xxxxxxxxxx,Class Method,Static,mvPublic,mkClassProcedure,dkStatic,-12,FClassProcedurePublicField
Method: xxxxxxxxxx,xxxxxxxxxx,Class Method,Static,mvPublished,mkClassProcedure,dkStatic,-12,FClassProcedurePublishedField
Method: xxxxxxxxxx,xxxxxxxxxx,Class Method,xxxxxx,mvPublic,mkClassFunction,dkStatic,-12,ClassInfo
Method: xxxxxxxxxx,xxxxxxxxxx,Class Method,xxxxxx,mvPublic,mkClassFunction,dkStatic,-12,ClassName
Method: xxxxxxxxxx,xxxxxxxxxx,Class Method,xxxxxx,mvPublic,mkClassFunction,dkStatic,-12,ClassNameIs
Method: xxxxxxxxxx,xxxxxxxxxx,Class Method,xxxxxx,mvPublic,mkClassFunction,dkStatic,-12,ClassParent
Method: xxxxxxxxxx,xxxxxxxxxx,Class Method,xxxxxx,mvPublic,mkClassFunction,dkStatic,-12,GetInterfaceEntry
Method: xxxxxxxxxx,xxxxxxxxxx,Class Method,xxxxxx,mvPublic,mkClassFunction,dkStatic,-12,GetInterfaceTable
Method: xxxxxxxxxx,xxxxxxxxxx,Class Method,xxxxxx,mvPublic,mkClassFunction,dkStatic,-12,InheritsFrom
Method: xxxxxxxxxx,xxxxxxxxxx,Class Method,xxxxxx,mvPublic,mkClassFunction,dkStatic,-12,InitInstance
Method: xxxxxxxxxx,xxxxxxxxxx,Class Method,xxxxxx,mvPublic,mkClassFunction,dkStatic,-12,InstanceSize
Method: xxxxxxxxxx,xxxxxxxxxx,Class Method,xxxxxx,mvPublic,mkClassFunction,dkStatic,-12,MethodAddress
Method: xxxxxxxxxx,xxxxxxxxxx,Class Method,xxxxxx,mvPublic,mkClassFunction,dkStatic,-12,MethodAddress
Method: xxxxxxxxxx,xxxxxxxxxx,Class Method,xxxxxx,mvPublic,mkClassFunction,dkStatic,-12,MethodName
Method: xxxxxxxxxx,xxxxxxxxxx,Class Method,xxxxxx,mvPublic,mkClassFunction,dkStatic,-12,QualifiedClassName
Method: xxxxxxxxxx,xxxxxxxxxx,Class Method,xxxxxx,mvPublic,mkClassFunction,dkStatic,-12,UnitName
Method: xxxxxxxxxx,xxxxxxxxxx,Class Method,xxxxxx,mvPublic,mkClassFunction,dkStatic,-12,UnitScope
Method: xxxxxxxxxx,xxxxxxxxxx,Class Method,xxxxxx,mvPublic,mkClassFunction,dkVtable,-3,NewInstance
Method: xxxxxxxxxx,xxxxxxxxxx,xxxxxxxxxxxx,xxxxxx,mvPublic,mkFunction,dkStatic,-12,ClassType
Method: xxxxxxxxxx,xxxxxxxxxx,xxxxxxxxxxxx,xxxxxx,mvPublic,mkFunction,dkStatic,-12,FieldAddress
Method: xxxxxxxxxx,xxxxxxxxxx,xxxxxxxxxxxx,xxxxxx,mvPublic,mkFunction,dkStatic,-12,FieldAddress
Method: xxxxxxxxxx,xxxxxxxxxx,xxxxxxxxxxxx,xxxxxx,mvPublic,mkFunction,dkStatic,-12,GetInterface
Method: xxxxxxxxxx,xxxxxxxxxx,xxxxxxxxxxxx,xxxxxx,mvPublic,mkFunction,dkVtable,-10,GetHashCode
Method: xxxxxxxxxx,xxxxxxxxxx,xxxxxxxxxxxx,xxxxxx,mvPublic,mkFunction,dkVtable,-11,Equals
Method: xxxxxxxxxx,xxxxxxxxxx,xxxxxxxxxxxx,xxxxxx,mvPublic,mkFunction,dkVtable,-8,SafeCallException
Method: xxxxxxxxxx,xxxxxxxxxx,xxxxxxxxxxxx,xxxxxx,mvPublic,mkFunction,dkVtable,-9,ToString
Method: xxxxxxxxxx,xxxxxxxxxx,xxxxxxxxxxxx,xxxxxx,mvPublic,mkProcedure,dkStatic,-12,CleanupInstance
Method: xxxxxxxxxx,xxxxxxxxxx,xxxxxxxxxxxx,xxxxxx,mvPublic,mkProcedure,dkStatic,-12,DisposeOf
Method: xxxxxxxxxx,xxxxxxxxxx,xxxxxxxxxxxx,xxxxxx,mvPublic,mkProcedure,dkStatic,-12,FProcedurePublicField
Method: xxxxxxxxxx,xxxxxxxxxx,xxxxxxxxxxxx,xxxxxx,mvPublic,mkProcedure,dkStatic,-12,Free
Method: xxxxxxxxxx,xxxxxxxxxx,xxxxxxxxxxxx,xxxxxx,mvPublic,mkProcedure,dkVtable,-2,FreeInstance
Method: xxxxxxxxxx,xxxxxxxxxx,xxxxxxxxxxxx,xxxxxx,mvPublic,mkProcedure,dkVtable,-4,DefaultHandler
Method: xxxxxxxxxx,xxxxxxxxxx,xxxxxxxxxxxx,xxxxxx,mvPublic,mkProcedure,dkVtable,-5,Dispatch
Method: xxxxxxxxxx,xxxxxxxxxx,xxxxxxxxxxxx,xxxxxx,mvPublic,mkProcedure,dkVtable,-6,BeforeDestruction
Method: xxxxxxxxxx,xxxxxxxxxx,xxxxxxxxxxxx,xxxxxx,mvPublic,mkProcedure,dkVtable,-7,AfterConstruction
Method: xxxxxxxxxx,xxxxxxxxxx,xxxxxxxxxxxx,xxxxxx,mvPublished,mkProcedure,dkStatic,-12,FProcedurePublishedField

Properties:
mvPublic,PrivatedField
mvPublic,ProtectedField
mvPublic,PublicatedField
mvPublic,PublishedField

 

Edited by programmerdelphi2k
  • Thanks 1

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×