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
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 {...} ...
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ìm kiếm theo từ khóa liên quan:
thủ thuật máy tính tài liệu công nghệ thông tin lập trình máy tính mẹo máy tính cài đặt máy tínhTài liệu liên quan:
-
Top 10 mẹo 'đơn giản nhưng hữu ích' trong nhiếp ảnh
11 trang 332 0 0 -
Làm việc với Read Only Domain Controllers
20 trang 323 0 0 -
Thêm chức năng hữu dụng cho menu chuột phải trên Windows
4 trang 307 0 0 -
70 trang 267 1 0
-
Bài giảng Tin học lớp 11 bài 1: Giới thiệu ngôn ngữ lập trình C#
15 trang 249 0 0 -
Tổng hợp lỗi Win 8 và cách sửa
3 trang 234 0 0 -
Sửa lỗi các chức năng quan trọng của Win với ReEnable 2.0 Portable Edition
5 trang 227 0 0 -
Phần III: Xử lý sự cố Màn hình xanh
3 trang 222 0 0 -
Tổng hợp 30 lỗi thương gặp cho những bạn mới sử dụng máy tính
9 trang 215 0 0 -
Sao lưu dữ liệu Gmail sử dụng chế độ Offline
8 trang 212 0 0