加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

是否可以在Delphi方法参数中使用属性?

发布时间:2020-12-15 10:10:31 所属栏目:大数据 来源:网络整理
导读:这是有效的代码与更新的Delphi版本? // handle HTTP request "example.com/products?ProductID=123"procedure TMyRESTfulService.HandleRequest([QueryParam] ProductID: string); 在此示例中,参数“ProductID”归因于[QueryParam].如果这是Delphi中的有效
这是有效的代码与更新的Delphi版本?
// handle HTTP request "example.com/products?ProductID=123"
procedure TMyRESTfulService.HandleRequest([QueryParam] ProductID: string);

在此示例中,参数“ProductID”归因于[QueryParam].如果这是Delphi中的有效代码,还必须有一种方法来编写基于RTTI的代码来查找归因参数类型信息.

看到我上一个问题Which language elements can be annotated using attributes language feature of Delphi?,其中列出了一些已报告使用属性的语言元素.该列表中缺少参数的属性.

解决方法

是的你可以:
program Project1;

{$APPTYPE CONSOLE}

uses
  Rtti,SysUtils;

type
  QueryParamAttribute = class(TCustomAttribute)
  end;

  TMyRESTfulService = class
    procedure HandleRequest([QueryParam] ProductID: string);
  end;

procedure TMyRESTfulService.HandleRequest(ProductID: string);
begin

end;

var
  ctx: TRttiContext;
  t: TRttiType;
  m: TRttiMethod;
  p: TRttiParameter;
  a: TCustomAttribute;
begin
  try
    t := ctx.GetType(TMyRESTfulService);
    m := t.GetMethod('HandleRequest');
    for p in m.GetParameters do
      for a in p.GetAttributes do
        Writeln('Attribute "',a.ClassName,'" found on parameter "',p.Name,'"');
  except
    on E: Exception do
      Writeln(E.ClassName,': ',E.Message);
  end;
  Readln;
end.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读