首页 > 其他分享 >Delphi注解(不是注释)

Delphi注解(不是注释)

时间:2022-12-21 18:55:31浏览次数:39  
标签:TMyAttribute end string Delphi Vcl 注释 LAttr 注解 Font

开发环境Delphi XE10

 

 

 

 

 

 1 unit Unit1;
 2 
 3 interface
 4 
 5 uses
 6   Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
 7   Vcl.Controls, Vcl.Forms, Vcl.Dialogs, System.RTTI, Vcl.StdCtrls;
 8 
 9 type
10   TForm1 = class(TForm)
11     Button1: TButton;
12     procedure Button1Click(Sender: TObject);
13   private
14     { Private declarations }
15   public
16     { Public declarations }
17   end;
18   type
19   TMyAttribute = class(TCustomAttribute)
20     private
21       FValue: string;
22     public
23        constructor Create(v: string); //带RTTI信息的Attributes时,必须要实现自定义的构造器
24       
25       property Value: string Read Fvalue Write FValue;
26    end;
27 //然后就可以用来注释了,简单的注释操作如下
28   [TMyAttribute('Msg')]
29   MyRec = record
30     R_str:string;
31   end;
32 var
33   Form1: TForm1;
34 
35 implementation
36 
37 {$R *.dfm}
38 function GetMyRecAttr: string;
39 var
40   LContext: TRttiContext;
41   LType: TRttiType;
42   LAttr: TCustomAttribute;
43 begin
44   LContext := TRTTIContext.Create; //创建上下文对象
45   LType := LContext.GetType(TypeInfo(MyRec));//获取MyRec的类型信息
46   try
47     for LAttr In LType.GetAttributes() do //遍历MyRec中的Attributes
48       if LAttr is TMyAttribute then
49         ShowMessage(TMyAttribute(LAttr).value);
50   except
51     ShowMessage('读取失败');
52   end;
53 end;
54 { TMyAttribute }
55 
56 constructor TMyAttribute.Create(v: string);
57 begin
58   FValue:='AAAAAAAAAAA+'+v;
59 end;
60 
61 procedure TForm1.Button1Click(Sender: TObject);
62 begin
63   GetMyRecAttr;
64 end;
65 
66 end.

 

 

 

 1 object Form1: TForm1
 2   Left = 0
 3   Top = 0
 4   Caption = 'Form1'
 5   ClientHeight = 130
 6   ClientWidth = 288
 7   Color = clBtnFace
 8   Font.Charset = DEFAULT_CHARSET
 9   Font.Color = clWindowText
10   Font.Height = -11
11   Font.Name = 'Tahoma'
12   Font.Style = []
13   OldCreateOrder = False
14   Position = poDesktopCenter
15   PixelsPerInch = 96
16   TextHeight = 13
17   object Button1: TButton
18     Left = 96
19     Top = 48
20     Width = 75
21     Height = 25
22     Caption = 'Button1'
23     TabOrder = 0
24     OnClick = Button1Click
25   end
26 end

 

标签:TMyAttribute,end,string,Delphi,Vcl,注释,LAttr,注解,Font
From: https://www.cnblogs.com/dmqhjp/p/16996942.html

相关文章

  • Spring提取@Transactional事务注解的源码解析
    Spring提取@Transactional事务注解的源码解析声明:本文是自己在学习​​spring​​注解事务处理源代码时所留下的笔记;难免有错误,敬请读者谅解!!!1、事务注解标签<tx:annotatio......
  • 一篇文章彻底明白java中的重要概念——注解
    1.注解基本概念 注解,什么是注解? 打开百度搜索     好,看不懂没关系一步一步慢慢来先不管注解,注释这个概念应该就很熟悉了,文档注释,单行注释,多行注释......
  • mybatis注解开发
    @Insert:实现新增@Update:实现更新@Delete:实现删除@Select:实现查询@Result:实现结果集封装@Results:可以与@Result一起使用,封装多个结果集@ResultMap:实现引用@Re......
  • delphi11安卓权限的改变
    delphi11安卓权限的改变///<author>cxg2022-12-21</author>unituRights;interfaceusesSystem.Permissions,System.SysUtils,System.Types,System.UITyp......
  • Delphi 经典游戏程序设计40例 的学习 例40 动画检查器
     unitR40;interfaceusesWindows,Messages,SysUtils,Variants,Classes,Graphics,Controls,Forms,Dialogs,Buttons,StdCtrls,Clipbrd,ExtCtrls;......
  • 注解的那些事儿(二)| 如何自定义注解
    自定义注解是自己写框架的必备技能,使用注解能极大地提升开发效率,因此自定义注解是一个高级开发者必备的技能。要自定义注解,首先需要了解一个注解的构成部分。一个注解大致可......
  • delphi 遭遇Frame与ActionList结合出现的坑
    我有个frame,里面有一批按钮默认visible设置false 然后在调用时,按实际需要,显示出需要的按钮  然后我又加入了一个ActionList控件  ,然后问题就来了....当......
  • SpringBoot - 条件注解 @Conditional
    @ConditiOnBean作用:如果Spring容器里面存在指定的Bean则生效范围:类上,方法上,一般在配置类中使用参数:value参数类型Class[],name参数类型String[]IOC容器中组件的名称......
  • SpringBoot - 配置包扫描注解@ComponentScan
    @ComponentScan作用:配置包扫描规则范围:主程序类上(被@SpringBootApplication修饰),或配置类上(被@Configuration修饰)参数:value指定要扫描的包,excludeFilters配置排除......
  • SpringBoot - MVC三层架构注解注入到容器中与从IOC容器获取实例注解
    MVC三层架构注解@Controller控制层@Service业务层@Repository持久层@Component作用:把类注入到IOC容器当中范围:类上参数:value给类起类名从IOC容器中获取实例注......