惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

T
Threat Research - Cisco Blogs
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
V
Vulnerabilities – Threatpost
GbyAI
GbyAI
P
Proofpoint News Feed
L
LINUX DO - 热门话题
P
Palo Alto Networks Blog
A
About on SuperTechFans
T
Tenable Blog
M
MIT News - Artificial intelligence
IT之家
IT之家
I
Intezer
D
DataBreaches.Net
爱范儿
爱范儿
T
Threatpost
C
CERT Recently Published Vulnerability Notes
云风的 BLOG
云风的 BLOG
博客园 - 三生石上(FineUI控件)
WordPress大学
WordPress大学
K
Kaspersky official blog
大猫的无限游戏
大猫的无限游戏
A
Arctic Wolf
Y
Y Combinator Blog
Cyberwarzone
Cyberwarzone
酷 壳 – CoolShell
酷 壳 – CoolShell
D
Darknet – Hacking Tools, Hacker News & Cyber Security
H
Help Net Security
Microsoft Security Blog
Microsoft Security Blog
Spread Privacy
Spread Privacy
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
AWS News Blog
AWS News Blog
博客园 - 聂微东
C
Check Point Blog
S
Securelist
有赞技术团队
有赞技术团队
雷峰网
雷峰网
aimingoo的专栏
aimingoo的专栏
Last Week in AI
Last Week in AI
Stack Overflow Blog
Stack Overflow Blog
MongoDB | Blog
MongoDB | Blog
D
Docker
G
GRAHAM CLULEY
T
The Exploit Database - CXSecurity.com
C
Cybersecurity and Infrastructure Security Agency CISA
T
Tailwind CSS Blog
L
Lohrmann on Cybersecurity
G
Google Developers Blog
C
Cyber Attacks, Cyber Crime and Cyber Security
L
LangChain Blog

博客园 - Tim.Tang

个人简历常用词汇(zt) 听懂新闻英语的十大方法(zt) 传说中的100句英语可以帮你背7000单词(zt) spring 与 guice 的区别好玩的好法(转) Popular Verbal English Basketball English Popular English Sentences 今天中午的时候公司里的一个老大跟我说给我争取一个去参加候捷设计模式培训的名额 近期关于“软件架构”的一个看点(zt) 动态控件的状态问题的分析 JavaScript事件串联之土办法 计算机科学经典论文(转载) 一个IT项目经理的心情日记(转载) 怎样做项目计划(转载) 微软资深经理人的项目管理经验(转载) 软件项目过程管理经验(转载) 项目管理过程中要注意的一些问题 如何在应用Object DataSource和Typed DataSet构建的应用程序中使用水晶报表 如果应用Object DataSource和Typed DataSet快速构建三层结构的B/S应用程序 - 原创
Attributes of C Sharp
Tim.Tang · 2007-04-08 · via 博客园 - Tim.Tang

Definition:  An attribute is a piece of additional declarative information that is specified for a declaration (MSDN).

So, we can look Attribute like a Decorator from the definition.

Example:

 [Obsolete("This method is substituted by xxx", true)]

public void Demo() { ... }

param1: 为什么该元素被荒弃,以及我们该使用什么元素来代替它.

param2: 告诉编译器把依然使用这被标识的元素视为一种错误,这就意味着编译器会因此而产生一个警告

Develop Customizd Attribute:

A class that derives from the abstract class System.Attribute, whether directly or indirectly, is an attribute class. The declaration of an attribute class defines a new kind of attribute that can be placed on a declaration (MSDN).

必选参数和命名参数

必选参数:在Contructor中进行初始化

命名参数:未在Contructor中初始化,但定义了Set方法的Property.

参数类型:simple data type, System.Type, object

public class MyAttribute : Attribute
{
protected string description;
protected string version;
public MyAttribute(string description)
{
this.description = description;
}
public string Description
{
get { return this.description;}
set { this.description = value;}
}
public string Version
{
get { return this.version;}
set { this.version = value;}
}
}
使用:
[MyAttribute("This is a class", Version = "1.0", Description = "This is a class 2")]
public class TestClass
{
...
}

此时

MyAttribute.Description  : This is a class 2  //先在构造时被设为This is a class, 后又被设为 ... 2

MyAttribute.Version : 2.0

约束自定义Attribute的使用

AttributeUsage 类:  描述了一个自定义attribute类能被怎样使用. attribute类本身用这个atrribute System.AttributeUsage来标记.

AttributeUsage的三个属性:

1. ValidOn: 指定约束的attribute可以应用在哪些语言元素上. 可选的值有:AttributeTargets. {Assembly, Moudle, Class, Struct, Enum, Constructor, Method, Property, Field, Event, Interface, Parameter, Delegate, All, ClassMembers = (Class | Struct | Enum | Constructor | Method | Property | Field | Event | Delegate | Interface)}default: AttributeTargets.All

2. AllowMultiple: 指示约束的attribute是否可在同一语言元素上使用多次. default: false

3. Inherited: 指示约束的attribute是否被派生类继承. default: false

示例:

1. Attribute Definition

using system;
[AttributeUsage(AttributeTargets.Class, AllowMultiple = false, Inherited = false )]
public class MyAttribute : Attribute
{
public MyAttribute(string description){...}
...
}

2. Using

[MyAttribute("This is MyClass")]
[MyAttribute("This is MyClass")] //Error: because AllowMultiple = false
public class MyClass
{
[MyAttribute("This is a Method")] //Error: AttributeTarges.Class specified that we just can use this attribute on a class
public void Show() {...}
}

Attribute标记

功能: 约束将Attribute应用到哪一个语言元素

可用标识符: assembly, moudle, type, method, property, event, field, param, return

功能示例:

1. Attribute 放在哪儿才能让编译器确定该attribute是绑定至整个assembly?

2. 怎样才能让编译器确定我们是把attribute绑定至方法的返回类型上,而不是整个方法?

[assembly: MyAttribute(...)]

运行时查询Attributes (By Reflection)

1. 查询程序集的custom attribute

class TestApp
{
public static void Main()
{
Process p = Process.GetCurrentProcess();
string assemblyName = p.ProcessName + ".exe";
Assembly a = Assembly.LoadFrom(assemblyName);
foreach (Attribute attr in a.GetCustomAttributes(true))
{
if(typeof(MyAttribute).IsAssignableFrom(typeof(attr)))
{
Console.Write("Description of {0}: {1}", assemblyName, attr.Description);
}
}
}
}

2. 查询class, method, property的attributes

  a) class:  type.GetCustomAttributes(true) 得到所有绑定的attributes -> ...

  b) method type.GetMethods() -> method.GetCustomAttributes(true) -> ...

  c) property  type.GetFields() -> field.GetCustomAttributes(true) -> ...

典型Attributes应用(待整理)

额外阅读推荐:

组件常用的设计期与解析期元数据Attribute