AttributeUsage

  System.AttributeUsage声明一个Attribute的使用范围与使用原则。

  

  AllowMultiple 和 Inherited 参数是可选的,所以此代码具有相同的效果:

  

  AttributeTarget的值可以参考1。部分可取值如下:

  

  如果 AllowMultiple 参数设置为 true,则返回特性可对单个实体应用多次。

  如果 Inherited 设置为 false,则该特性不由从特性化的类派生的类继承。

  Attribute.GetCustomAttribute可以获取一个类的Attribute。

[AttributeUsage(AttributeTargets.Class)]
public class VersionAttribute : Attribute
{
public string Name { get; set; }
public string Date { get; set; }
public string Describtion { get; set; }
} [Version(Name = "hyddd", Date = "2009-07-20", Describtion = "hyddd's class")]
public class MyCode
{
//...
} class Program
{
static void Main(string[] args)
{
var info = typeof(MyCode);
var classAttribute = (VersionAttribute)Attribute.GetCustomAttribute(info, typeof(VersionAttribute));
Console.WriteLine(classAttribute.Name);
Console.WriteLine(classAttribute.Date);
Console.WriteLine(classAttribute.Describtion);
}
}

参考:

1、http://msdn.microsoft.com/zh-cn/library/system.attributetargets.aspx

2、http://msdn.microsoft.com/zh-cn/library/tw5zxet9.aspx

3、http://www.cnblogs.com/hyddd/archive/2009/07/20/1526777.html

AttributeUsage的更多相关文章

  1. [C#]Attribute特性(3)——AttributeUsage特性和特性标识符

    相关文章   [C#]Attribute特性 [C#]Attribute特性(2)——方法的特性及特性参数 AttributeUsage特性 除了可以定制自己的特性来注释常用的C#类型外,您可以用At ...

  2. C# AttributeUsage的使用浅析

    C# AttributeUsage的使用是如何的呢?首先让我们来了解一下什么是AttributeUsage类它是另外一个预定义特性类,AttributeUsage类的作用就是帮助我们控制定制特性的使用 ...

  3. [0] 自定义特性AttributeUsage

    using System;using System.Collections.Generic;using System.Linq;using System.Text; namespace Fxframe ...

  4. 理解AttributeUsage类

    类定义: // 摘要: // 指定另一特性类的用法. 此类不能被继承. [Serializable] [AttributeUsage(AttributeTargets.Class, Inherited ...

  5. C#提供-------AttributeUsage使用

    Assembly(c#中简单说明) Assembly(c#中简单说明) 2008-07-11 00:27 什么是Assembly(程序集)?Assembly是一个包含来程序的名称,版本号,自我描述,文 ...

  6. Xamarin+Prism开发详解六:DependencyService与IPlatformInitializer的关系

    祝各位2017年事业辉煌!开年第一篇博客,继续探索Xamarin.Forms… 为什么我做Xamarin开发的时候中意于Prism.Forms框架?本章为你揭晓. 实例代码地址:https://git ...

  7. .NET里简易实现AOP

    .NET里简易实现AOP 前言 在MVC的过滤器章节中对于过滤器的使用就是AOP的一个实现了吧,时常在工作学习中遇到AOP对于它的运用可以说是很熟练了,就是没想过如果自己来实现的话是怎么实现的,性子比 ...

  8. C# 中参数验证方式的演变

    一般在写方法的时候,第一步就是进行参数验证,这也体现了编码者的细心和缜密,但是在很多时候这个过程很枯燥和乏味,比如在拿到一个API设计文档的时候,通常会规定类型参数是否允许为空,如果是字符可能有长度限 ...

  9. xamarin DependencyService源码阅读

    xamarin在面对PCL无法实现的各平台特有功能时使用了一种叫[DependencyService]的方式来实现.它使得xamarin能像原生平台一样做平台能做到的事情!主要分四个部分 接口:定义功 ...

随机推荐

  1. Python猴子补丁

    属性在运行时的动态替换,叫做猴子补丁(Monkey Patch). 为什么叫猴子补丁 属性的运行时替换和猴子也没什么关系,关于猴子补丁的由来网上查到两种说法: 1,这个词原来为Guerrilla Pa ...

  2. webpack 图片资源处理

    备注:  css 引用图片资源 1. 安装loader yarn add file-loader --dev 2. 配置 const path = require("path"); ...

  3. 怎样创建Linux Initrd

    Linux初始RAM磁盘(initrd)是在系统引导过程中挂载的一个暂时根文件系统,用来支持两阶段的引导过程.initrd文件里包括了各种可运行程序和驱动程序.它们能够用来挂载实际的根文件系统,然后再 ...

  4. CDN初学搭建(ats)

    CDN初学搭建(ats) ats trafficserver squid 一. CDN初学搭建 准备vagrant virtualbox 内部环境测试所需包 一.vagrant创建启动虚拟机 1 mk ...

  5. JDK1.5java新特性

    JDK1.5java增加的新特性: 自动装箱/拆箱      增强for     泛型      枚举      静态导入      可变参数 1 自动装箱/拆箱 * JDK1.5允许开发人员把一个基 ...

  6. TCP 、UDP、IP包的最大长度

    1.概述 首先要看TCP/IP协议,涉及到四层:链路层,网络层,传输层,应用层. 其中以太网(Ethernet)的数据帧在链路层 IP包在网络层 TCP或UDP包在传输层 TCP或UDP中的数据(Da ...

  7. [rejected] master -> master (fetch first)(non-fast forward)

    git push 时候遇到的错误: hint: Updates were rejected because the tip of your current branch is behind hint: ...

  8. [模拟赛]异或最大值 maxinum

    此题在考试时用暴力,暴了30分. 献上30分代码: #include<stdio.h> ]; int main() { int n,t,c,i,max,j,d; freopen(" ...

  9. GOF23设计模式之外观模式(facade)

    一.外观模式概述 外观模式也称为门面模式. 核心:为了系统提供统一的入口,封装子系统的复杂性,便于客户端调用. 二.外观模式场景导入与示例代码 场景:要想自己去注册一个公司,首先去工商局检测命名是否合 ...

  10. 学生选课数据库SQL语句练习题

    一.            设有一数据库,包括四个表:学生表(Student).课程表(Course).成绩表(Score)以及教师信息表(Teacher).四个表的结构分别如表1-1的表(一)~表( ...