Jump to content

Leaderboard


Popular Content

Showing content with the highest reputation on 01/01/23 in Posts

  1. programmerdelphi2k

    Access class fields and properties with RTTI

    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-}
  2. 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.
×