使用 RTTI 访问严格的私有字段

发布于 2024-12-05 20:07:37 字数 863 浏览 1 评论 0原文

考虑这个简单的代码

{$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.

当您运行此代码时,仅列出 field3。看来RTTI不支持严格私有class var字段,所以问题是是否可以使用访问delphi类的严格私有字段Rtti 或其他方法? 我阅读了 RTTI.TRttiType.GetFields 方法,但确实提到了这些限制,存在提到此类限制的任何论文或文章吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

九局 2024-12-12 20:07:37

我现在无法尝试,但您似乎需要的是 GetDeclaredFields 而不是 GetFields。这应该给出类的所有(实例)字段,但不给出祖先类的字段。如果您也需要这些,则必须递归地沿着继承链向上。

正如我所说,我现在无法尝试,所以你必须亲自看看它是否也能让你访问严格的私有字段。

更新

请注意,在您的 TFoo 声明中,即使您可能无意,Field1 和 Field2 都是类变量!

只需重新格式化您的声明,您就会明白我的意思:

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

class var 之后的所有内容都是类变量,直到编译器遇到 varstrict< /em>、私有受保护等。试试这个,您还会看到Field2被写入:

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

或者尝试:

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

这意味着GetFields 和 GetDeclaredFields 没有任何严格私有字段的问题。他们只是不返​​回类变量。这是有道理的,国际海事组织。类变量不是正在研究的对象的成员。

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.

总以为 2024-12-12 20:07:37

使用类助手

请参阅 access-a-strict-protected-property -of-a-delphi-class 作为一个工作示例。

相关还有如何使用类的地址和偏移量来访问类变量的值-变量,其中类助手用于访问严格的私有类变量。

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.

花桑 2024-12-12 20:07:37

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文