通过RTTI调用受保护的方法(构造函数) [英] Call a protected method (constructor) via RTTI

查看:418
本文介绍了通过RTTI调用受保护的方法(构造函数)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用XE-2。



可以使用RTTI调用受保护的方法(构造函数)吗?



我在网上搜索,但没有找到任何确凿的答案。我明白,在XE之前,只有发布的方法/属性才可用。我确实有对私有字段的写入权限,所以我期望能够调用受保护的方法。



只要构造函数为public,以下代码就可以工作。 p>

 函数GetDefaultConstructor(aRttiType:TRttiType):TRttiMethod; 
var
方法:TRttiMethod;
begin
for aRttiType.GetMethods('Create')中的方法do
begin
if(Method.IsConstructor)和(length(Method.GetParameters)= 0)和(Method .Parent = aRttiType)then
Exit(Method);
结束
结果:= nil;
结束


解决方案

RTTI信息增加可执行文件的大小。因此,设计人员为开发人员提供了一种手段来指定要将多少RTTI信息链接到可执行文件。



默认情况下,没有链接保护方法的RTTI。所以你必须指定你想要添加此RTTI。例如,这个程序

  {$ APPTYPE CONSOLE} 

使用
System.TypInfo ,System.Rtti;

type
TMyClass = class
protected
构造函数创建;
结束

构造函数TMyClass.Create;
begin
end;

var
ctx:TRttiContext;
方法:TRttiMethod;对于ctx.GetType(TMyClass)中的方法,

begin
。如果method.Visibility = mvProtected则为
。如果方法为
Writeln(method.Name);
结束。

不产生输出。但是,这个程序

  {$ APPTYPE CONSOLE} 

使用
System.TypInfo,系统

type
{$ RTTI EXPLICIT METHODS([vcProtected])}
TMyClass = class
protected
constructor Create;
结束

构造函数TMyClass.Create;
begin
end;

var
ctx:TRttiContext;
方法:TRttiMethod;对于ctx.GetType(TMyClass)中的方法,

begin
。如果method.Visibility = mvProtected则为
。如果方法为
Writeln(method.Name);
结束。

输出

 
创建

有关详细信息,请参阅这些文档主题:




am using XE-2.

Is it possible to invoke a protected method (constructor) using RTTI?

I searched the web but did not find any conclusive answer. I understand that prior to XE only published methods/properties were available. I do have write access to private fields, so I was expecting to be able to invoke protected methods.

The following code works as long as the constructor is public.

function GetDefaultConstructor(aRttiType: TRttiType): TRttiMethod;
var
   Method: TRttiMethod;
begin
   for Method in aRttiType.GetMethods('Create') do
   begin
      if (Method.IsConstructor) and (length(Method.GetParameters) = 0) and (Method.Parent = aRttiType) then
         Exit(Method);
   end;
   Result := nil;
end;

解决方案

RTTI information increases the size of executable files. Because of this the designers provided a means for the developer to specify how much RTTI information is to be linked to the executable.

By default, no RTTI for protected methods is linked. So you have to specify that you want this RTTI to be added. For example, this program

{$APPTYPE CONSOLE}

uses
  System.TypInfo, System.Rtti;

type
  TMyClass = class
  protected
    constructor Create;
  end;

constructor TMyClass.Create;
begin
end;

var
  ctx: TRttiContext;
  method: TRttiMethod;

begin
  for method in ctx.GetType(TMyClass).GetMethods do
    if method.Visibility=mvProtected then
      Writeln(method.Name);
end.

produces no output. However, this program

{$APPTYPE CONSOLE}

uses
  System.TypInfo, System.Rtti;

type
  {$RTTI EXPLICIT METHODS([vcProtected])}
  TMyClass = class
  protected
    constructor Create;
  end;

constructor TMyClass.Create;
begin
end;

var
  ctx: TRttiContext;
  method: TRttiMethod;

begin
  for method in ctx.GetType(TMyClass).GetMethods do
    if method.Visibility=mvProtected then
      Writeln(method.Name);
end.

outputs

Create

For more information, refer to these documentation topics:

这篇关于通过RTTI调用受保护的方法(构造函数)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆