Danh mục

Compaq C Language Reference Manual_9

Số trang: 26      Loại file: pdf      Dung lượng: 218.25 KB      Lượt xem: 16      Lượt tải: 0    
tailieu_vip

Hỗ trợ phí lưu trữ khi tải xuống: 7,000 VND Tải xuống file đầy đủ (26 trang) 0

Báo xấu

Xem trước 3 trang đầu tiên của tài liệu này:

Thông tin tài liệu:

Tham khảo tài liệu compaq c language reference manual_9, công nghệ thông tin, kỹ thuật lập trình phục vụ nhu cầu học tập, nghiên cứu và làm việc hiệu quả
Nội dung trích xuất từ tài liệu:
Compaq C Language Reference Manual_9 Chapter 16 Exceptions16. ExceptionsCopyright  Microsoft Corporation 1999-2000. All Rights Reserved. 249 Chapter 17 Attributes17. AttributesMuch of the C# language enables the programmer to specify declarative information about the entities definedin the program. For example, the accessibility of a method in a class is specified by decorating it with themethod-modifiers public, protected, internal, and private.C# enables programmers to invent new kinds of declarative information, to specify declarative information forvarious program entities, and to retrieve attribute information in a run-time environment. For instance, aframework might define a HelpAttribute attribute that can be placed on program elements such as classesand methods to provide a mapping from program elements to documentation for them.New kinds of declarative information are defined through the declaration of attribute classes (§17.1), which mayhave positional and named parameters (§17.1.2). Declarative information is specified a C# program usingattributes (§17.2), and can be retrieved at run-time as attribute instances (§17.3).17.1 Attribute classesThe declaration of an attribute class defines a new kind of attribute that can be placed on a declaration. A classthat derives from the abstract class System.Attribute, whether directly or indirectly, is an attribute class.A declaration of an attribute class is subject to the following additional restrictions:• A non-abstract attribute class must have public accessibility.• All of the types in which a non-abstract attribute class is nested must have public accessibility.• A non-abstract attribute class must have at least one public constructor. • Each of the formal parameter types for each of the public constructors of an attribute class must be an attribute parameter type (§17.1.3).By convention, attribute classes are named with a suffix of Attribute. Uses of an attribute may either includeor omit this suffix. AttributeUsage17.1.1 The AttributeUsage att ributeThe AttributeUsage attribute is used to describe how an attribute class can be used.The AttributeUsage attribute has a positional parameter named that enables an attribute class to specify thekinds of declarations on which it can be used. The example [AttributeUsage(AttributeTargets.Class | AttributeTargets.Interface)] public class SimpleAttribute: System.Attribute {}defines an attribute class named SimpleAttribute that can be placed on class-declarations and interface-declarations. The example [Simple] class Class1 {…} [Simple] interface Interface1 {…}shows several uses of the Simple attribute. The attribute is defined with a class named SimpleAttribute, butuses of this attribute may omit the Attribute suffix, thus shortening the name to Simple. The example aboveis semantically equivalent to the example [SimpleAttribute] class Class1 {…}Copyright  Microsoft Corporation 1999-2000. All Rights Reserved. 251C# LANGUAGE REFERENCE [SimpleAttribute] interface Interface1 {…}The AttributeUsage attribute has an AllowMultiple named parameter that specifies whether the indicatedattribute can be specified more than once for a given entity. An attribute that can be specified more than once onan entity is called a multi-use attribute class. An attribute that can be specified at most once on an entity iscalled a single-use attribute class.The example [AttributeUsage(AttributeTargets.Class, AllowMultiple = true)] public class AuthorAttribute: System.Attribute { public AuthorAttribute(string value); public string Value { get {…} } }defines a multi-use attribute class named AuthorAttribute. The example [Author(Brian Kernighan), Author(Dennis Ritchie)] class Class1 {…}shows a class declaration with two uses of the Author attribute.17.1.2 Positional and named p arametersAttribute classes can have positional parameters and named parameters. Each public constructor for an attributeclass defines a valid sequence of positional parameters for the attribute class. Each non-static public read-writefield and property for an attribute class defines a named parameter for the attribute class.The example [AttributeUsage(AttributeTargets.Class] public class HelpAttribute: System.Attribute { public HelpAttribute(string url) { // url is a positional parameter … } public string Topic { // Topic is a named parameter get {...} ...

Tài liệu được xem nhiều: