Delphi:写入后代类中私有祖先的字段
我需要修复第三方组件。该组件的类具有私有变量,该变量由其后代主动使用:
TThirdPartyComponentBase = class
private
FSomeVar: Integer;
public
...
end;
TThirdPartyComponent = class (TThirdPartyComponentBase)
protected
procedure Foo; virtual;
end;
procedure TThirdPartyComponent.Foo;
begin
FSomeVar := 1; // ACCESSING PRIVATE FIELD!
end;
这是有效的,因为两个类位于同一单元中,因此它们有点“朋友”。
但是,如果我尝试在新单元中创建新类,
TMyFixedComponent = class (TThirdPartyComponent)
procedure Foo; override;
end;
我将无法再访问 FSomeVar,但我需要使用它来进行修复。我真的不想在我的代码中重现所有基类树。
如果可能的话,您能否建议一些快速的技巧来访问该私有字段而不更改原始组件的单位?
I need to fix a third-party component. This component's class has private variable which is actively used by its descendants:
TThirdPartyComponentBase = class
private
FSomeVar: Integer;
public
...
end;
TThirdPartyComponent = class (TThirdPartyComponentBase)
protected
procedure Foo; virtual;
end;
procedure TThirdPartyComponent.Foo;
begin
FSomeVar := 1; // ACCESSING PRIVATE FIELD!
end;
This works because both classes are in the same unit, so they're kinda "friends".
But if I'll try to create a new class in a new unit
TMyFixedComponent = class (TThirdPartyComponent)
procedure Foo; override;
end;
I can't access FSomeVar anymore, but I need to use it for my fix. And I really don't want to reproduce in my code all that tree of base classes.
Can you advise some quick hack to access that private field without changing the original component's unit if it's possible at all?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
通过使用类帮助器,可以实现从派生类访问基类的私有部分,而不会失去类型安全性。
只需在另一个单元中添加这些声明即可:
从代码中的注释可以看出,
FSomeVar
由TThirdPartyComponentBase
类中的类帮助程序公开。TThirdPartyComponent
的另一个类帮助器实现了 Foo 过程。在那里,对基类助手的SomeVar
属性的访问是通过类型转换到基类来实现的。By the use of
class helpers
it's possible to accomplish access to the private parts of the base class from the derived class without loosing type safety.Just add these declarations in another unit:
As can be seen from comments in code,
FSomeVar
is exposed by a class helper from theTThirdPartyComponentBase
class.Another class helper for the
TThirdPartyComponent
implements the Foo procedure. In there, access to theSomeVar
property of the base class helper is made via a type cast to the base class.您必须使用 hack 来访问不同单元中任何类(包括基类)中的私有字段。在您的情况下,在您的单元中定义:
然后获取访问权限:
当然,这是危险的,因为您需要控制基类中的更改。因为如果字段布局发生变化而您会错过这个事实,那么上述方法将导致失败、AV 等。
You have to use a hack to access a private field in any class (including a base class) in a different unit. In your case define in your unit:
Then get the access:
Of course, that is dangerous, because you will need to control changes in the base class. Because if the fields layout will be changed and you will miss this fact, then the above approach will lead to failures, AV's, etc.
不知道这是否有帮助,但我似乎记得有一种方法可以将私有变量“破解”为可见性。
例如,我知道,当我将属性从较低的可见性(在基类中)移动到更可见的级别(在我的后代中)时,我遇到了来自编译器的警告。该警告指出,它正在以不同的可见性级别进行声明...
已经有一段时间了,我不确定,但我相信您可以做的是在您的后代中将相同的变量声明为受保护的。 (您可能必须使用 Redeclare 关键字才能进行编译。)
抱歉,我没有关于如何执行此操作的更多具体信息(如果确实可能的话)。也许这篇文章会提示这里的向导之一纠正我! :-)
Don't know if this will help, but I seem to recall there is a way to "crack" a private variable into visibility.
I know, for example, I've encountered warnings from the compiler when I've moved a property from lower visibility (in the base class) to a more visible level (in my descendant). The warning stated that it's being declared at a different level of visibility...
It's been some time and I'm not certain, but I believe what you can do is in your descendant declare the same variable as protected. (You may have to use the Redeclare keyword for this to compile.)
Sorry I don't have more specific information on how to do this (if it's indeed possible.) Perhaps this posting will prompt one of the wizards here into correcting me! :-)
通过 TThirdPartyComponent 中的受保护属性公开私有变量的值。
在
TMyFixedComponent
类中,在您想要覆盖的过程中使用MyVar
属性。Expose the value of the private variable by a protected property in TThirdPartyComponent.
In
TMyFixedComponent
class use theMyVar
Property in the procedure which you would like to override.