• Delphi Annotation属性注释


    Attributes是Delphi中的一个特点。它用于对某些对象(如类、记录、接口等)进行注释,并携带一些RTTI用于查询

    Attributes本身不会改变注释对象的任何行为

    Attributes注释的对象必须是运行期可见的,如public或Published

    声明一个Attributes类型,首先要继承自TCustomAttribute,并且不能是抽象对象

     type
      TMyAttribute = class(TCustomAttribute)
        private
          FValue: string;
        public
          property Value: string Read Fvalue Write FValue;
          constructor Create(Const v: string); //带RTTI信息的Attributes时,必须要实现自定义的构造器
          //并且构造器中的参数只能是常量或常量表达式
       end;
    //然后就可以用来注释了,简单的注释操作如下
      [TMyAttribute('Msg')]
      MyRec = record
          ...
      end;
    
    

    如果有两个Attributes的类名前缀相同,如TMyAtt和TMyAttribute, 在使用[TMyAtt]进行注释时,用到的是TMyAttribute 而不是TMyAtt

    获取注释的RTTI信息

    uses
      System.RTTI;
    
    function GetMyRecAttr: string;
    var
      LContext: TRttiContext;
      LType: TRttiType;
      LAttr: TCustomAttribute;
    begin
      LContext := TRTTIContext.Create; //创建上下文对象
      LType := LContext.GetType(TypeInfo(MyRec));//获取MyRec的类型信息
      try
        for LAttr In LType.Attributes() do //遍历MyRec中的Attributes
          if LAttr is TMyAttribute then
            Writeln(TMyAttribute(LAttr).value);
      except
        Writeln('读取失败'); //使用try except结构
      end;
    end;
    
  • 相关阅读:
    Go网络文件传输
    Go网络编程
    LNMP环境搭建(PHP7.4.0)
    LNMP环境搭建(PHP7.2.25)
    Please ensure the argon2 header and library are installed
    MySQL权限管理
    nginx ingress controller配置默认SSL证书
    kubernetes pod内抓包,telnet检查网络连接的几种方式
    ansible取出register变量中最长字符串
    kubernetes flannel pod CrashLoopBackoff解决
  • 原文地址:https://www.cnblogs.com/wuchangxing/p/15637274.html
Copyright © 2020-2023  润新知