6

consider this simple code

{$APPTYPE CONSOLE}

uses
  Rtti,
  SysUtils;

type
  {$M+}
  TFoo = class
  strict private
    class var Field1 : Integer;
    field2 :  Integer;
  private
    field3 :  Integer;
    class var Field4 : Integer;
  end;


Var
    ctx : TRttiContext;
    f   : TRttiField;
begin
  try
    ctx:=TRttiContext.Create;

    for f in ctx.GetType(TFoo).GetFields do
     Writeln(f.Name);


    Writeln('Done');
    readln;
  except
    on E: Exception do
      Writeln(E.ClassName, ': ', E.Message);
  end;
end.

When you run this, only the field3 is listed. it seems which the RTTI does not support fields which are strict private or class var, So the questions are Is possible access a strict private field of a delphi class using Rtti or another method? and I read the documentation of the RTTI.TRttiType.GetFields method but does mention these restrictions, Exist any paper or article which mentions such limitations?

Salvador
  • 16,132
  • 33
  • 143
  • 245

3 Answers3

6

I can't try it right now, but what you seem to need could be GetDeclaredFields instead of GetFields. This should give all (instance) fields of a class but not those of an ancestor class. If you need those too, you'll have to recursively go up the inheritance chain.

As I said, I can't try it right now, so you'll have to see for yourself if it gives you access to strict private fields as well.

Update

Note that in your declaration of TFoo, even you probably didn't intend it, both Field1 and Field2 are class variables!.

Just reformat your declaration, and you'll see what I mean:

  TFoo = class
  strict private
    class var
      Field1: Integer;
      Field2: Integer;
  private
    // etc...

Everything that comes after class var is a class variable, until the compiler encounters var, strict, private, protected, etc. Try this, and you'll also see Field2 being written:

  TFoo = class
  strict private
    class var 
      Field1: Integer;
    var 
      Field2: Integer;
    // etc...

Alternatively try:

  TFoo = class
  strict private
    Field2: Integer;
    class var 
      Field1: Integer;
    // etc...

This means that GetFields and GetDeclaredFields don't have any problems with strict private fields. They just don't return class variables. That makes sense, IMO. Class variables are not members of the object being investigated.

Rudy Velthuis
  • 28,387
  • 5
  • 46
  • 94
  • the `GetDeclaredFields` function return the same result in this case. – Salvador Sep 23 '11 at 14:45
  • Bummer! It should return more. – Rudy Velthuis Sep 23 '11 at 21:59
  • 1
    Take a look at the update in my answer. Both *GetFields* and *GetDeclaredFields* work fine, also for *strict private*, but they don't return class variables, since these are, well, not fields of the object. – Rudy Velthuis Sep 23 '11 at 22:50
  • +1 for __Note that in your declaration of TFoo, even you probably didn't intend it, both Field1 and Field2 are class variables!.__ – Whiler Sep 24 '11 at 00:18
  • Thanks. just a final question what you mean with `..Class variables are not members of the object being investigated.`? – Salvador Sep 24 '11 at 21:03
  • @Salvador: Class variables are, well, variables of the class (they only exist once, as part of the class), not fields of an object. So it makes sense they are not listed as fields. – Rudy Velthuis Sep 24 '11 at 21:08
3

Access to strict private members of a class is possible with Class Helpers.

See access-a-strict-protected-property-of-a-delphi-class for a working example.

Related is also how-can-access-the-value-of-a-class-var-using-the-address-of-the-class-and-a-offset-to-the-variable, where class helpers is used to access a strict private class var.

Community
  • 1
  • 1
LU RD
  • 34,438
  • 5
  • 88
  • 296
2

By definition, strict private is only visible in the scope of the class itself. They should still be accessible with Hallvard's hack #5, though (except for class fields, I think).

Ondrej Kelle
  • 36,941
  • 2
  • 65
  • 128