AttributeUsage属性

article/2025/9/30 4:20:31
除了定制 attributes 之外,可以使用 Attributes 属性定义如何使用这些属性。例如:<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

[AttributeUsage( 
    validon, 
    AllowMultiple = allowmultiple, 
    Inherited = inherited
)] 

强烈推荐使用AttributeUsage属性将属性文档化,因此属性的用户能直接使用已命名的属性,而不用在源代码中查找公用的读/写字段和属性。

定义属性目标

 1 None.gif public   enum  AttributeTargets
 2 ExpandedBlockStart.gifContractedBlock.gif dot.gif {
 3InBlock.gif    Assembly    = 0x0001,
 4InBlock.gif    Module      = 0x0002,
 5InBlock.gif    Class       = 0x0004,
 6InBlock.gif    Struct      = 0x0008,
 7InBlock.gif    Enum        = 0x0010,
 8InBlock.gif    Constructor = 0x0020,
 9InBlock.gif    Method      = 0x0040,
10InBlock.gif    Property    = 0x0080,
11InBlock.gif    Field       = 0x0100,
12InBlock.gif    Event       = 0x0200,
13InBlock.gif    Interface   = 0x0400,
14InBlock.gif    Parameter   = 0x0800,
15InBlock.gif    Delegate    = 0x1000,
16InBlock.gif    All = Assembly │ Module │ Class │ Struct │ Enum │ Constructor │ 
17InBlock.gif        Method │ Property │ Field │ Event │ Interface │ Parameter │ 
18InBlock.gif        Delegate,
19InBlock.gif
20InBlock.gif    ClassMembers  =  Class │ Struct │ Enum │ Constructor │ Method │ 
21InBlock.gif        Property │ Field │ Event │ Delegate │ Interface,
22ExpandedBlockEnd.gif}

23 None.gif

当使用 Attribute 属性时,能指定 AttributeTargets.all (属性目标),因此属性能被附加到在枚举 AttributeTargets 列出的任意类型上。若未指定 AttributeUsage 属性,缺省值是 AttributeTargets.All 。属性 AttributeTargets 用来限制属性使用范围。

 1 None.gif [AttributeUsage(AttributeTargets.Class)]
 2 None.gif public   class  RemoteObjectAttribute : Attribute
 3 ExpandedBlockStart.gifContractedBlock.gif dot.gif {
 4InBlock.gif
 5ExpandedBlockEnd.gif}

 6 None.gif
 7 None.gif[AttributeUsage(AttributeTargets.Method)]
 8 None.gif public   class  TransactionableAttribute : Attribute
 9 ExpandedBlockStart.gifContractedBlock.gif dot.gif {
10InBlock.gif
11InBlock.gif
12InBlock.gif
13ExpandedBlockEnd.gif}

14 None.gif

  可以使用或( | )操作符组合属性目标枚举中列出的项。

 单一用途和多用途属性

可以使用AttributeUsage定义属性的单一用途或多用途。即确定在单个字段上使用单一属性的次数。在缺省情况下,所有属性都是单用途的。在AttributeUsage属性中,指定AllowMultipletrue,则允许属性多次附加到指定的类型上。例如:

 1 None.gif [AttributeUsage(AttributeTargets.All, AllowMultiple = true )]
 2 None.gif public   class  SomethingAttribute : Attribute
 3 ExpandedBlockStart.gifContractedBlock.gif dot.gif {
 4InBlock.gif    public SomethingAttribute(String str)
 5ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
 6ExpandedSubBlockEnd.gif    }

 7ExpandedBlockEnd.gif}

 8 None.gif
 9 None.gif[Something( " abc " )]
10 None.gif[Something( " def " )]
11 None.gif class  MyClass
12 ExpandedBlockStart.gifContractedBlock.gif dot.gif {
13ExpandedBlockEnd.gif}

14 None.gif

 

指定继承属性规则

   AttributeUsageAttribute属性的最后部分是继承标志,用于指定属性是否能被继承。缺省值是false。然而,若继承标志被设置为true,它的含义将依赖于AllowMultiple标志的值。若继承标志被设置为true,并且AllowMultiple标志是flag,则改属性将忽略继承属性。若继承标志和AllowMultiple标志都被设置为true,则改属性的成员将与派生属性的成员合并。范例:

 1 None.gif using  System;
 2 None.gif using  System.Reflection;
 3 None.gif
 4 None.gif namespace  AttribInheritance
 5 ExpandedBlockStart.gifContractedBlock.gif dot.gif {
 6InBlock.gif    [AttributeUsage(
 7InBlock.gif        AttributeTargets.All, 
 8InBlock.gif        AllowMultiple = true,
 9InBlock.gif        //AllowMultiple = false,
10InBlock.gif        Inherited = true
11InBlock.gif    )]
12InBlock.gif    public class SomethingAttribute : Attribute
13ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
14InBlock.gif        private string name;
15InBlock.gif        public string Name
16ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif
17ExpandedSubBlockStart.gifContractedSubBlock.gif            get dot.gifreturn name; }
18ExpandedSubBlockStart.gifContractedSubBlock.gif            set dot.gif{ name = value; }
19ExpandedSubBlockEnd.gif        }

20InBlock.gif
21InBlock.gif        public SomethingAttribute(string str)
22ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
23InBlock.gif            this.name = str;
24ExpandedSubBlockEnd.gif        }

25ExpandedSubBlockEnd.gif    }

26InBlock.gif
27InBlock.gif     [Something("abc")]
28InBlock.gif    class MyClass
29ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
30ExpandedSubBlockEnd.gif    }

31InBlock.gif
32InBlock.gif    [Something("def")]
33InBlock.gif    class Another : MyClass
34ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
35ExpandedSubBlockEnd.gif    }

36InBlock.gif
37InBlock.gif    class Test
38ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
39InBlock.gif        [STAThread]
40InBlock.gif        static void Main(string[] args)
41ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
42InBlock.gif            Type type =
43InBlock.gif                Type.GetType("AttribInheritance.Another");
44InBlock.gif            foreach (Attribute attr in type.GetCustomAttributes(true))
45InBlock.gif                //type.GetCustomAttributes(false))
46ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
47InBlock.gif                SomethingAttribute sa = 
48InBlock.gif                    attr as SomethingAttribute;
49InBlock.gif                if (null != sa)
50ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
51InBlock.gif                    Console.WriteLine(
52InBlock.gif                        "Custom Attribute: {0}"
53InBlock.gif                        sa.Name);
54ExpandedSubBlockEnd.gif                }

55ExpandedSubBlockEnd.gif            }

56InBlock.gif
57ExpandedSubBlockEnd.gif        }

58ExpandedSubBlockEnd.gif    }

59ExpandedBlockEnd.gif}

60 None.gif

 

AllowMultiple设置为false,结果是:

Custom Attribute: def

 AllowMultiple设置为true,结果是:

Custom Attribute: def

Custom Attribute: abc

转载于:https://www.cnblogs.com/stzyw/archive/2005/10/07/249693.html


http://chatgpt.dhexx.cn/article/t5Lgm2bv.shtml

相关文章

AttributeUsage

AttributeUsage 【AttributeUsage】 System.AttributeUsage声明一个Attribute的使用范围与使用原则。 AllowMultiple 和 Inherited 参数是可选的&#xff0c;所以此代码具有相同的效果&#xff1a; AttributeTarget的值可以参考1。部分可取值如下&#xff1a; 如果 AllowMultip…

吉大软构件和中间件课程-JPA配置方法

吉大软构件和中间件课程-JPA配置方法 JPA连接方法&#xff1a; 1.standalone.xml 文字&#xff1a; <xa-datasource jta"true" jndi-name"java:jboss/datasources/MySqlDS" pool-name"MySqlDS" enabled"true" use-java-c…

JPA 配置文件最详细总结,没有之一!

又来了一个懵懂少年&#xff0c;看我怎么骗你的。 来我们开始学习吧。 PropertyPlaceholderConfigure 载入属性文件&#xff1a; 例如&#xff1a;class"org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"><propertyname"loc…

Spring Boot使用spring-data-jpa配置Mysql多数据源

转载请注明出处 :Spring Boot使用spring-data-jpa配置Mysql多数据源 我们在之前的文章中已经学习了Spring Boot中使用mysql数据库 在单数据源的情况下&#xff0c;Spring Boot的配置非常简单&#xff0c;只需要在application.properties文件中配置连接参数即可。 但是往往随…

SpringBoot 配置 JPA

新建项目 在application.properties配置文件中进行配置&#xff08;或者application.yaml中配置也行&#xff09; spring.datasource.urljdbc:mysql://localhost:3306/ssm?characterEncodingutf8&useSSLfalse&serverTimezoneUTC spring.datasource.usernameroot sp…

Springboot---JPA配置

1.在配置文件中写入数据库信息 application.properties配置如下 spring.datasource.primary.urljdbc:mysql://localhost:3306/test1 spring.datasource.primary.usernameroot spring.datasource.primary.passwordroot spring.datasource.primary.driver-class-namecom.mysql.…

spring-boot-starter-data-jpa 配置多个数据源与jpa实体类继承的问题、分页条件查询

JPA的继承注解一般有四种 MappedSuperclass 这个注解应用的场景是父类不对应任何单独的表&#xff0c;多个子类共用相同的属性。 注意&#xff1a; MappedSuperclass注解使用在父类上面&#xff0c;是用来标识父类的作用 MappedSuperclass标识的类表示其不能映射到数据库表&am…

Springboot多数据源+Jpa配置

随着业务复杂程度的增加&#xff0c;单一数据源越来越不满足具体的业务逻辑以及实现。 这里我用到了MySQL和Presto两种数据源&#xff1a; 多数据源配置GlobalDataSourceConfiguration&#xff1a; Configuration public class GlobalDataSourceConfiguration {Bean(name …

springboot--jpa 配置多数据库

使用spring boot jpa 配置多数据源 由于项目整合 以前的功能 但是以前功能存储的数据库是另一个数据库 这两天搜索了一下 遇见了许多坑 在这里记录一下 首先附上我的项目结构 可能有些乱 忘见谅。 pom.xml(把数据库的依赖引入) <!-- mariadb --><dependen…

Spring Data Jpa 配置多数据源

文章目录 1.配置数据库连接信息2.编写数据源配置类3.编写数据库配置4.目录结构 1.配置数据库连接信息 spring:datasource:db1: # 1.0 Datasourceurl: jdbc:mysql://127.0.0.1:3306/test1?useSSLfalse&serverTimezoneGMT%2b8&characterEncodingutf8&connectTimeo…

springboot2+JPA 配置多数据源(不同类型数据库)

注意&#xff1a;看此篇文章之前&#xff0c;springbootjpa的配置环境应搭建好&#xff0c;不会搭可以自行百度。本文章主要讲述配置JPA多数据源。 1.数据源配置文件 application.properties # 数据源thirded&#xff08;oracle数据库&#xff09; spring.jpa.thirded.databa…

jpa配置(jpa配置连接池)

JPA的实体状态有哪些呢&#xff1f; 该接口拥有众多执行数据查询的接口方法&#xff1a; Object getSingleResult()&#xff1a;执行SELECT查询语句&#xff0c;并返回一个结果&#xff1b; List getResultList() &#xff1a;执行SELECT查询语句&#xff0c;并返回多个结果&…

SpringBoot系列之数据库初始化-jpa配置方式

上一篇博文介绍如何使用spring.datasource来实现项目启动之后的数据库初始化&#xff0c;本文作为数据库初始化的第二篇&#xff0c;将主要介绍一下&#xff0c;如何使用spring.jpa的配置方式来实现相同的效果 I. 项目搭建 1. 依赖 首先搭建一个标准的SpringBoot项目工程&am…

Jpa环境配置及入门(增删改查)

案例&#xff1a;客户的相关操作&#xff08;增删改查&#xff09; 1.分析&#xff1a; 1.搭建环境&#xff1a; 创建maven工程&#xff0c;导入相关坐标&#xff1b; 配置使用jpa的核心配置文件&#xff1b; 位置&#xff1b;需要配置到类路径下叫做 META-INF的文件夹下 命…

PHP多国语言开发:CodeIgniter 2PHP框架中的多国语言,语言包(i18n)库

PHP多国语言开发&#xff1a;CodeIgniter 2PHP框架中的多国语言&#xff0c;语言包&#xff08;i18n&#xff09;多国语言库 引言 我们在CodeIgniter开发中经常会碰到多国语言网站&#xff0c;这里我们就来介绍一种简单有效的多国语言的操作方法。 做什么 语言在地址中是这…

Win 10 添加多国语言

不同用户对电脑系统的语言需求也不一样&#xff0c;出于工作原因需要使用其它语言&#xff0c;比如外国友人需要使用英语&#xff0c;俄罗斯语言等&#xff0c;此时很多用户都以为要下载对应语言版本的系统&#xff0c;然后重新安装系统&#xff0c;其实Win10是支持多国语言的&…

手工编译Flex SDK 多国语言包

项目需要将目前版本提供给其它地区&#xff1a;台湾、日韩等&#xff0c;面临着项目语言的国际化问题。 语言代号&#xff1a; 大陆&#xff1a;zh_CN 台湾&#xff1a;zh_TW 香港&#xff1a;zh_HK … 例如想支持繁体&#xff0c;没有zh_TW语言包怎么办&#xff1f; fl…

DevExpress去除多国语言包

DevExpress作为windows开发中较为强大的第三方组件&#xff0c;能极大的提高编程效率和界面效果。但也要引用它较多的dll文件&#xff0c;它专门有个查看dll程序集依赖的工具&#xff0c;在VS的工具菜单下&#xff1a; 在VS的工具菜单内有"DevExpress Assembly Deploymen…

关于VS编译DevExpress默认产生几个多余的多国语言包的问题解决

关于VS编译DevExpress默认产生几个多余的多国语言包的问题解决 VS15开始对于非系统的Dll都会默认复制到本地&#xff0c;即bin\debug下面&#xff0c;复制dll到本地好处在于发布的时候不用再去寻找相关dll,对于dev这么庞大的组件来说&#xff0c;更是如此&#xff0c;当然&…

php源码添加多国语言包,为win7系统添加多国语言包的方法

现在使用win7系统的人越来越多了&#xff0c;对于一些需求也是有所增长&#xff0c;很多用户希望能够将自己的操作系统安装成英文&#xff0c;法文&#xff0c;德文等语言&#xff0c;尤其是对经常出去外国出处的用户很有好吃&#xff0c;比如要和外国客户沟通交流时能看的懂自…