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

推荐订阅源

腾讯CDC
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
博客园 - Franky
博客园_首页
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
IT之家
IT之家
The Cloudflare Blog
V
Visual Studio Blog
罗磊的独立博客
T
Tailwind CSS Blog
S
SegmentFault 最新的问题
Hugging Face - Blog
Hugging Face - Blog
V
V2EX
阮一峰的网络日志
阮一峰的网络日志
D
Docker
Last Week in AI
Last Week in AI
B
Blog RSS Feed
C
Check Point Blog
J
Java Code Geeks
The GitHub Blog
The GitHub Blog
有赞技术团队
有赞技术团队
博客园 - 聂微东
MongoDB | Blog
MongoDB | Blog
雷峰网
雷峰网

博客园 - bengxia

DataGridView中的{索引-1沒有值}异常 Silverlight + WCF + Sharepoint(Silverlight Web Part) 问题集合 CLR Via C# 3rd 阅读摘要 -- Chapter 29 – Hybrid Thread Synchronization Constructs CLR Via C# 3rd 阅读摘要 -- Chapter 28 – Primitive Thread Synchronization Constructs FIX: Unable to find a version of the runtime to run this application. CLR Via C# 3rd 阅读摘要 -- Chapter 27 – I/O-Bound Asynchronous Operations CLR Via C# 3rd 阅读摘要 -- Chapter 26 – Compute-Bound Asynchronous Operations CLR Via C# 3rd 阅读摘要 -- Chapter 25 – Thread Basics CLR Via C# 3rd 阅读摘要 -- Chapter 24 – Runtime Serialization CLR Via C# 3rd 阅读摘要 -- Chapter 23 – Assembly Loading and Reflection CLR Via C# 3rd 阅读摘要 -- Chapter 22 – CLR Hosting and AppDomains CLR Via C# 3rd 阅读摘要 -- Chapter 21 – Automatic Memory Management (Garbage Collection) CLR Via C# 3rd 阅读摘要 -- Chapter 20 – Exceptions and State Management - bengxia CLR Via C# 3rd 阅读摘要 -- Chapter 19 – Nullable Value Types CLR Via C# 3rd 阅读摘要 -- Chapter 17 - Delegates CLR Via C# 3rd 阅读摘要 -- Chapter 16 - Arrays - bengxia CLR Via C# 3rd 阅读摘要 -- Chapter 15 - Enumerated Types and Bit Flags CLR Via C# 3rd 阅读摘要 -- Chapter 14 - Chars, Strings, and Working with Text CLR Via C# 3rd 阅读摘要 -- Chapter 13 - Interfaces
CLR Via C# 3rd 阅读摘要 -- Chapter 18 - Custom Attributes
bengxia · 2010-04-20 · via 博客园 - bengxia

Using Custom Attributes

  1. CLR允许Attributes应用在可以在文件的元数据中的任何玩意上。比如:TypeDef(classes,structures,enumerations,interfaces,and delegates),MethodDef(包括constructors),ParamDef,FieldDef,PropertyDef,EventDef,AssemblyDef,ModuleDef。此外,可以在源代码上面应用Attributes的目标:程序集,模块,类型(class,struct,enum,interface,delegate),字段,方法(包括constructors),方法参数,方法的返回值,属性,事件,泛型类型参数;
  2. Attributes可用的特殊的前缀:assembly, moudle, type, field, return, method, param, property, event;
  3. C#只允许CLS兼容的Attributes。最终都是继承于System.Attribute;
  4. [DllImport(“Kernel32”, CharSet = CharSet.Auto, SetLastError = true)],构造器的参数Kernel32…叫做positional parameters并且是强制的。CharSet…叫做named parameters是可选的;
  5. 一个目标上是可以应用多个Attribute,但是要注意顺序。而且"Attribute"后缀是可选的。

Define Your Own Attribute Class

  1. 如何定义自己的Attribute,参考FlagsAttribute:
    namespace System {
         public class FlagsAttribute : System.Attribute {
             public FlagsAttribute() {
             }
         }
    }
  2. 可以把Attribute看做是一个逻辑状态容器;
  3. System.AttributeUsageAttribute属性:AllowMultiple,Inherited,ValidOn(readonly) Attribute可应用的目标参考System.AttributeTargets枚举;
  4. 关于Attribute的继承。.NET仅考虑这些目标:class, method, property, event, field, method return value, parameter;
  5. 如果定义Attributes时不指定AttributeUsage,那么CLR假定:可以应用到所有目标;只能应用一次;继承的。

Attribute Constructor and Field/Property Data Types

  1. 在Attribute中可用的数据类型子集:Boolean, Char, Byte, SByte, Int16, UInt16, Int32, UInt32, Int64, UInt64, Single, Double, String, Type, Object,或者枚举类型。还可以使用这些类型的一维零基的数组。但是要避免使用数组,因为不适CLS兼容的;
  2. 使用Attribute时,必须传入编译时常量表达式;
  3. 如果是Type的参数,字段,或属性,必须使用C#的typeof操作符;
  4. 逻辑上,如果编译器检测出定制的Attribute应用在一个目标上,编译器会通过调用Attribute类的构造函数产生出一个实例;
  5. 定制的Attribute:类的实例已经被序列化成字节流驻留在元数据中。然后在运行时反序列化。

Detecting the Use of a Custom Attribute

  1. System.Attribute的IsDefined, GetCustomAttributes, GetCustomAttribute。IsDefined用来检测,GetCustomAttributes和GetCustomAttribute可以用来构造一个Attribute对象;
  2. 这些操作都很费时间,少用微妙。而且,如果用的频繁,应该考虑缓存;
  3. 在定制自己的Attributes时应该考虑标记为sealed,减轻潜在的混淆以及消除额外的检查。

Matching Two Attribute Instances Against Each Other

  1. System.Attribute覆盖了Object.Equals方法,代替以检查两个对象的类型,如果类型不同返回false,否则通过反射比较两个Attribute对象的字段。所以,最好考虑自己重写Equals,以避免使用反射;
  2. System.Attribute还提供了Match方法来表示更丰富的语义,但是默认实现是调用Equals。

Detecting the Use of a Custom Attribute Without Creating Attribute-Derived Objects

  1. System.Reflection.CustomAttributeData.GetCustomAttributes()作为一个工厂方法。

Conditional Attribute Classes

  1. 在Attribute类定义上使用ConditionalAttribute称为conditional attribute。

本章小结

  定制属性(Custom Attribute)是.NET框架中的一个重要革新。定制属性可以允许你以申明的方式标注代码结构。本章先讲了如何使用Attribute以及如何定义一个定制属性,然后解释了Attribute的构造器、字段、属性的数据类型限制,还演示了如何检测类型中使用的定制属性。特别说明了System.Attribute的Equals、Match的内部实现并建议用户改写这些方法以避免反射。还给出了一个不用创建继承属性对象并能检测定制属性的方法(System.Reflection.CustomAttributeData.GetCustomAttributes,最后说明了如何使用条件属性。