官网地址:https://docs.microsoft.com/zh-cn/dotnet/csharp/programming-guide/concepts/attributes/accessing-attributes-by-using-reflection

    /// <summary>
/// 使用特性,可以有效地将元数据或声明性信息与代码(程序集、类型、方法、属性等)相关联。 将特性与程序实体相关联后,可以在运行时使用反射这项技术查询特性
/// </summary>
///
[System.AttributeUsage(System.AttributeTargets.Class|System.AttributeTargets.Struct,AllowMultiple = true)]
public class Author : System.Attribute
{
string name;
public double version; public Author(string name)
{
this.name = name;
version = 1.0;
} public string GetName()
{
return name;
}
} // Class with the Author attribute.
[Author("P. Ackerman")]
public class FirstClass
{
// ...
} // Class without the Author attribute.
public class SecondClass
{
// ...
} // Class with multiple Author attributes.
[Author("P. Ackerman"), Author("R. Koch", version = 2.0)]
public class ThirdClass
{
// ...
} class TestAuthorAttribute
{
public static void Test()
{
PrintAuthorInfo(typeof(FirstClass));
PrintAuthorInfo(typeof(SecondClass));
PrintAuthorInfo(typeof(ThirdClass));
} private static void PrintAuthorInfo(System.Type t)
{
System.Console.WriteLine("Author information for {0}", t);
System.Attribute[] attrs = System.Attribute.GetCustomAttributes(t);
foreach(System.Attribute attr in attrs)
{
if(attr is Author)
{
Author a = (Author)attr;
System.Console.WriteLine(" {0}, version {1:f}", a.GetName(), a.version);
}
}
}
}

查看官网复习一下相关概念,随手记录一下,以便以后翻阅

C#基础之特性的更多相关文章

  1. day31 Pyhton 面向对象的基础 三大特性

    一.内容回顾 封装 1.概念 笔记 2.__名字 在类的外部就不能用了 3.私有化的 不能被子类继承,也不能在其他任何类中调用 三个装饰器方法(装饰类中的方法) 1.不被修饰的  普通方法,会使用对象 ...

  2. C# 基础知识-特性

    C基础 - 特性 一.特性 1>特性本质就是一个类,直接或者间接的继承了Attribute 2>特性就是在不破话类封装的前提下,加点额外的信息或者行为 特性添加后,编译会在元素内部产生IL ...

  3. python3.3 基础 新特性

    前段时间看到对 python 之父的采访,持 python应尽量使用新版的态度. 所以学习,就从比较新的版本开始吧. 3.x 之后的版本与2.x 的版本还是有些不同,仅从入门的基础部分即可感受到, 比 ...

  4. Python基础——3特性

    特性 切片 L=[0,1,2,3,4,5,6,7,8,9,10] L[:3]=[0,1,2] L[-2:]=[9,10] L[1:3]=[1,2] L[::3]=[0,3,6,9] L[:5:2]=[ ...

  5. 3-0 js基础 语言特性及性能优化

    1.语言特性: 内存泄露:内存没有释放,越堆越多. 垃圾回收(生命周期): 1.局部 很短 在局部中当函数完成时.已经释放了.全局变量在页面关闭的时候才被回收. 2.全局 很长 3.闭包.可长可短,只 ...

  6. [oldboy-django][5python基础][高级特性]generator生成器

    # 生成器基础 - 定义 在循环的时候不断推算下一个元素的值,而不是一下子创建空间存储所有元素,这样节省空间. 并且在适当的条件结束循环,这种一边循环一边计算的机制,称为generator生成器 - ...

  7. Java基础—面向对象特性

    1.三大特性 ①.封装 所谓封装,就是将客观事物封装成抽象的类,类的数据和方法只允许可信的类或者对象操作,对不可信的类或对象进行信息隐藏.封装是面向对象的特征之一,是对象和类概念的主要特性.简单的说, ...

  8. Java基础:特性write once;run anywhere!

    三高:高可用 高性能 高并发 特性: 简单性 面向对象:万物皆为对象 可移植性 高性能 分布式 动态性 多线程 安全性 健壮性 Java三大版本 javaSE:标准版(桌面程序,控制台) javaME ...

  9. HTTP 基础(特性、请求方法、状态码、字段)

    1. HTTP 简介(含义.特性.缺点) 2. HTTP 报文 3. GET 和 POST 4. 状态码 5. HTTP 头字段 1. HTTP 简介 HTTP 的含义 HTTP (HyperText ...

随机推荐

  1. 二分图——poj2239

    水题 /* n门课,每门课有一个时间t 要求最大的n->t的匹配 */ #include<iostream> #include<cstring> #include< ...

  2. SpringBoot学习笔记(三):SpringBoot集成Mybatis、SpringBoot事务管理、SpringBoot多数据源

    SpringBoot集成Mybatis 第一步我们需要在pom.xml里面引入mybatis相关的jar包 <dependency> <groupId>org.mybatis. ...

  3. c++ STL使用

    STL标准模板库,提供一些类似java集合类的数据结构容器.比如eque.list.vector.map 等.还提供一些支持这些容器的算法和遍历容器的迭代器. 使用方法 #include <io ...

  4. PAT甲级——A1073 Scientific Notation

    Scientific notation is the way that scientists easily handle very large numbers or very small number ...

  5. AOP的几种实现方法

    C# 实现AOP 的几种常见方式 原文出处:http://www.cnblogs.com/zuowj/p/7501896.html AOP为Aspect Oriented Programming的缩写 ...

  6. Python学习day14-函数进阶(2)

    figure:last-child { margin-bottom: 0.5rem; } #write ol, #write ul { position: relative; } img { max- ...

  7. 如何在easyui datagrid 中显示外键的值

    1.需要在角色列表显示角色类别的值,而角色类别是外键,关联数据字典,明细见下图: 2.代码如下: columns: [[ { field: 'ck', checkbox: true, align: ' ...

  8. PIL的ImageDraw的颜色问题

    因为我的图片的单通道的,所以用 draw = ImageDraw.Draw(image) im_width, im_height =], info[], info[], info[]) color=d ...

  9. 目标检测从入门到精通—SPP-Net详细解析(三)

    SPP-Net网络结构分析 Author:Mr. Sun Date:2019.03.18 Loacation: DaLian university of technology 论文名称:<Spa ...

  10. 嘴巴题4 「BZOJ1827」[Usaco2010 Mar] gather 奶牛大集会

    1827: [Usaco2010 Mar]gather 奶牛大集会 Description Bessie正在计划一年一度的奶牛大集会,来自全国各地的奶牛将来参加这一次集会.当然,她会选择最方便的地点来 ...