Attribute trong C | 85 bài học lập trình C hay nhất PDF

9 210 0
Attribute trong C  | 85 bài học lập trình C  hay nhất PDF

Đang tải... (xem toàn văn)

Thông tin tài liệu

http://vietjack.com/csharp/index.jsp Copyright © vietjack.com Attribute C# Một attribute C#, thẻ tường thuật, sử dụng để truyền thông tin tới runtime hành vi phần tử đa dạng lớp, phương thức, cấu trúc, enum, assembly, … chương trình bạn Bạn thêm thơng tin tường thuật tới chương trình việc sử dụng Attribute Một thẻ tường thuật miêu tả dấu ngoặc móc vng ([]) đặt bên phần tử mà sử dụng cho Các Attribute sử dụng để thêm metadata, ví dụ lệnh biên dịch thông tin khác comment, miêu tả, phương thức lớp tới chương trình .Net Framework cung cấp hai kiểu Attribute: Attribute định nghĩa trước Custom Attribute Xác định Attribute C# Cú pháp để xác định Attribute C# sau: [attribute(positional_parameters, name_parameter = value, )] element Tên Attribute giá trị xác định bên dấu ngoặc vng, trước phần tử từ thuộc tính áp dụng cho positional_parameters xác định thông tin thiết yếu vàname_parameter xác định thông tin tùy ý Attribute định nghĩa trước C# Net Framework cung cấp Attribute định nghĩa trước:  AttributeUsage  Conditional  Obsolete AttributeUsage C# Attribute định nghĩa trước AttributeUsage miêu tả cách lớp custom Attribute sử dụng Nó xác định kiểu item, mà từ Attribute áp dụng cho Cú pháp để xác định Attribute C# sau: [AttributeUsage( validon, http://vietjack.com/ Trang chia sẻ học online miễn phí Page http://vietjack.com/csharp/index.jsp Copyright © vietjack.com AllowMultiple=allowmultiple, Inherited=inherited )] Tại đây:  Tham số validon xác định phần tử ngơn ngữ mà Attribute đặt Nó tổ hợp giá trị AttributeTargets enumerator Giá trị mặc định làAttributeTargets.All  Tham số allowmultiple (tùy ý) cung cấp giá trị cho thuộc tính AllowMultiple attribute này, giá trị Boolean Nếu điều true, Attribute multiuse Giá trị mặc định false (tức single-use)  Tham số inherited (tùy ý) cung cấp giá trị cho thuộc tính Inherited attribute này, giá trị Boolean Nếu true, Attribute kế thừa lớp kế thừa Giá trị mặc định false (khơng kế thừa) Ví dụ: [AttributeUsage(AttributeTargets.Class | AttributeTargets.Constructor | AttributeTargets.Feild | AttributeTargets.Method | AttributeTargets.Property, AllowMultiple = true)] Conditional C# Attribute tiền định nghĩa đánh dấu phương thức có điều kiện mà thực thi phụ thuộc vào tiến trình tiền xử lý định danh cho Nó tạo biên dịch có điều kiện lời gọi phương thức, phụ thuộc vào giá trị cho, Debug Trace Ví dụ: hiển thị giá trị biến debug code Cú pháp để xác định Attribute C# sau: [Conditional( conditionalSymbol http://vietjack.com/ Trang chia sẻ học online miễn phí Page http://vietjack.com/csharp/index.jsp Copyright © vietjack.com )] Ví dụ: [Conditional("DEBUG")] Sau ví dụ minh họa Conditional C#: #define DEBUG using System; using System.Diagnostics; public class Myclass { [Conditional("DEBUG")] public static void Message(string msg) { Console.WriteLine(msg); } } class Test { static void function1() { Myclass.Message("In Function 1."); function2(); } static void function2() { Myclass.Message("In Function 2."); } public static void Main() { http://vietjack.com/ Trang chia sẻ học online miễn phí Page http://vietjack.com/csharp/index.jsp Copyright © vietjack.com Myclass.Message("In Main function."); function1(); Console.ReadKey(); } } Khi code biên dịch thực thi, cho kết quả: In Main function In Function In Function Obsolete C# Attribute tiền định nghĩa C# đánh dấu thực thể chương trình mà khơng nên sử dụng Nó cho bạn khả để thông báo cho compiler để loại bỏ phần tử target cụ thể Ví dụ, phương thức sử dụng lớp bạn muốn giữ lại phương thức cũ lớp này, bạn đánh dấu obsolete việc hiển thị thông báo phương thức nên sử dụng, thay cho phương thức cũ Cú pháp để xác định Attribute C# sau: [Obsolete( message )] [Obsolete( message, iserror )] Tại đây,  Tham số message chuỗi miêu tả lý item obsolete sử dụng thay cho  Tham số iserror giá trị Boolean Nếu giá trị true, compiler nên đối xử sử dụng item lỗi Giá trị mặc định false (tức compiler tạo warning) http://vietjack.com/ Trang chia sẻ học online miễn phí Page http://vietjack.com/csharp/index.jsp Copyright © vietjack.com Ví dụ sau minh họa obsolete C#: using System; public class MyClass { [Obsolete("Don't use OldMethod, use NewMethod instead", true)] static void OldMethod() { Console.WriteLine("It is the old method"); } static void NewMethod() { Console.WriteLine("It is the new method"); } public static void Main() { OldMethod(); } } Khi bạn cố biên dịch chương trình trên, compiler cho thông báo lỗi: Don't use OldMethod, use NewMethod instead Tạo Custom Attribute C# Còn gọi Attribute tùy biến hay Attribute người dùng tự định nghĩa .Net Framework cho phép tạo Custom Attribute mà sử dụng để lưu giữ thơng tin tường thuật thu nhận runtime Thơng tin liên quan tới phần tử target phụ thuộc vào chuẩn thiết kế yêu cầu ứng dụng Tạo sử dụng Custom Attribute C# bao gồm bước sau:  Khai báo Custom Attribute  Xây dựng Custom Attribute http://vietjack.com/ Trang chia sẻ học online miễn phí Page http://vietjack.com/csharp/index.jsp  Áp dụng Attribute phần tử chương trình target  Truy cập Attribute thơng qua Reflection Copyright © vietjack.com Bước cuối liên quan tới việc viết chương trình đơn giản để đọc qua metadata để tìm notation đa dạng Metadata liệu thông tin sử dụng để miêu tả liệu khác Chương trình nên sử dụng Reflection để truy cập Attribute runtime Chúng ta bàn luận điều chương tới Khai báo Custom Attribute C# Một Custom Attribute nên kế thừa từ lớp System.Attribute C# Ví dụ: //a custom attribute BugFix to be assigned to a class and its members [AttributeUsage(AttributeTargets.Class | AttributeTargets.Constructor | AttributeTargets.Field | AttributeTargets.Method | AttributeTargets.Property, AllowMultiple = true)] public class DeBugInfo : System.Attribute Trong code trên, khai báo Custom Attribute DeBugInfo Xây dựng Custom Attribute C# Chúng ta xây dựng Custom Attribute có tên DeBugInfo, mà lưu giữ thông tin thu việc debug chương trình Nó giữ thông tin sau:  Số hiệu code để bug  Tên lập trình viên, người nhận diện bug  Ngày review cuối code  Một thông báo dạng chuỗi để lưu giữ lưu ý lập trình viên Lớp DeBugInfo có thuộc tính private để lưu giữ thơng tin thuộc tính public để lưu giữ thơng báo Vì thế, số hiệu bug, tên lập trình viên, ngày review tham số vị trí tương ứng lớp DeBugInfo thông báo tham số tùy ý http://vietjack.com/ Trang chia sẻ học online miễn phí Page http://vietjack.com/csharp/index.jsp Copyright © vietjack.com Mỗi Attribute phải có constructor Các tham số vị trí tương ứng nên truyền thơng qua constructor Ví dụ sau minh họa lớp DeBugInfo trên: //a custom attribute BugFix to be assigned to a class and its members [AttributeUsage(AttributeTargets.Class | AttributeTargets.Constructor | AttributeTargets.Field | AttributeTargets.Method | AttributeTargets.Property, AllowMultiple = true)] public class DeBugInfo : System.Attribute { private int bugNo; private string developer; private string lastReview; public string message; public DeBugInfo(int bg, string dev, string d) { this.bugNo = bg; this.developer = dev; this.lastReview = d; } public int BugNo { get { return bugNo; } } http://vietjack.com/ Trang chia sẻ học online miễn phí Page http://vietjack.com/csharp/index.jsp Copyright © vietjack.com public string Developer { get { return developer; } } public string LastReview { get { return lastReview; } } public string Message { get { return message; } set { message = value; } } } Áp dụng Custom Attribute C# Custom Attribute C# áp dụng việc đặt trước target nó: [DeBugInfo(45, "Zara Ali", "12/8/2012", Message = "Return type mismatch")] [DeBugInfo(49, "Nuha Ali", "10/10/2012", Message = "Unused variable")] http://vietjack.com/ Trang chia sẻ học online miễn phí Page http://vietjack.com/csharp/index.jsp Copyright © vietjack.com class Rectangle { //member variables protected double length; protected double width; public Rectangle(double l, double w) { length = l; width = w; } [DeBugInfo(55, "Zara Ali", "19/10/2012", Message = "Return type mismatch")] public double GetArea() { return length * width; } [DeBugInfo(56, "Zara Ali", "19/10/2012")] public void Display() { Console.WriteLine("Length: {0}", length); Console.WriteLine("Width: {0}", width); Console.WriteLine("Area: {0}", GetArea()); } } Trong chương tới, thu hồi thông tin Attribute sử dụng đối tượng lớp Reflection C# http://vietjack.com/ Trang chia sẻ học online miễn phí Page ... code C pháp để x c định Attribute C# sau: [Conditional( conditionalSymbol http://vietjack.com/ Trang chia sẻ h c online miễn phí Page http://vietjack.com/csharp/index.jsp Copyright © vietjack.com... http://vietjack.com/csharp/index.jsp  Áp dụng Attribute phần tử chương trình target  Truy c p Attribute thơng qua Reflection Copyright © vietjack.com Bư c cuối liên quan tới vi c viết chương trình. .. static void Main() { http://vietjack.com/ Trang chia sẻ h c online miễn phí Page http://vietjack.com/csharp/index.jsp Copyright © vietjack.com Myclass.Message("In Main function."); function1(); Console.ReadKey();

Ngày đăng: 02/12/2017, 20:01

Mục lục

    Xác định một Attribute trong C#

    Attribute được định nghĩa trước trong C#

    Tạo Custom Attribute trong C#

    Khai báo một Custom Attribute trong C#

    Xây dựng Custom Attribute trong C#

    Áp dụng Custom Attribute trong C#

Tài liệu cùng người dùng

  • Đang cập nhật ...

Tài liệu liên quan