Trang chủ
Bài viết mới
Diễn đàn
Bài mới trên hồ sơ
Hoạt động mới nhất
VIDEO
Mùa Tết
Văn Học Trẻ
Văn Học News
Media
New media
New comments
Search media
Đại Học
Đại cương
Chuyên ngành
Triết học
Kinh tế
KHXH & NV
Công nghệ thông tin
Khoa học kĩ thuật
Luận văn, tiểu luận
Phổ Thông
Lớp 12
Ngữ văn 12
Lớp 11
Ngữ văn 11
Lớp 10
Ngữ văn 10
LỚP 9
Ngữ văn 9
Lớp 8
Ngữ văn 8
Lớp 7
Ngữ văn 7
Lớp 6
Ngữ văn 6
Tiểu học
Thành viên
Thành viên trực tuyến
Bài mới trên hồ sơ
Tìm trong hồ sơ cá nhân
Credits
Transactions
Xu: 0
Đăng nhập
Đăng ký
Có gì mới?
Tìm kiếm
Tìm kiếm
Chỉ tìm trong tiêu đề
Bởi:
Hoạt động mới nhất
Đăng ký
Menu
Đăng nhập
Đăng ký
Install the app
Cài đặt
Chào mừng Bạn tham gia Diễn Đàn VNKienThuc.com -
Định hướng Forum
Kiến Thức
- HÃY TẠO CHỦ ĐỀ KIẾN THỨC HỮU ÍCH VÀ CÙNG NHAU THẢO LUẬN Kết nối:
VNK X
-
VNK groups
| Nhà Tài Trợ:
BhnongFood X
-
Bhnong groups
-
Đặt mua Bánh Bhnong
CÔNG NGHỆ
Công Nghệ Thông Tin
Code
Tìm hiểu về Attribute trong C#
JavaScript is disabled. For a better experience, please enable JavaScript in your browser before proceeding.
You are using an out of date browser. It may not display this or other websites correctly.
You should upgrade or use an
alternative browser
.
Trả lời chủ đề
Nội dung
<blockquote data-quote="uocmo_kchodoi" data-source="post: 177596" data-attributes="member: 165510"><p><span style="font-size: 18px"><span style="color: rgb(0, 0, 0)">Attribute là một cơ chế được dùng để định nghĩa và khai báo các thông tin cần thiết, nhằm bổ sung và hỗ trợ khá nhiều chức năng liên quan đến việc soạn thảo, debug, cũng như biên dịch các chương trình.</span></span></p><p><span style="font-size: 18px"></span></p><p><span style="color: rgb(41, 105, 176)"><a href="https://vnkienthuc.com/forums/code.377/" target="_blank"><span style="font-size: 18px">Một attribute trong C#</span></a></span><span style="font-size: 18px"><span style="color: #000000">, là một thẻ tường thuật, được sử dụng để truyền thông tin tới runtime về các hành vi của các phần tử đa dạng như các lớp, phương thức, cấu trúc, enum, assembly, … trong chương trình của bạn. Bạn có thể thêm thông tin tường thuật tới một chương trình bởi việc sử dụng một Attribute. Một thẻ tường thuật được miêu tả bởi các dấu ngoặc móc vuông ([]) được đặt bên trên phần tử mà nó được sử dụng cho. Nhiều attribute có thể được sử dụng trong cùng một cặp ngoặc vuông này và được ngăn cách nhau bởi dấu phẩy “,”.</span></span></p><p><span style="font-size: 18px"></span></p><p><span style="color: rgb(61, 142, 185)"><a href="https://vnkienthuc.com/threads/tim-hieu-ve-reflection-trong-c.77353/" target="_blank"><span style="font-size: 18px">Các Attribute</span></a></span><span style="font-size: 18px"> <span style="color: #000000">được sử dụng để thêm metadata, ví dụ như chỉ lệnh biên dịch và thông tin khác như comment, miêu tả, phương thức và các lớp tới một chương trình. .Net Framework cung cấp hai kiểu Attribute: các Attribute được định nghĩa trước và các </span></span><span style="color: rgb(61, 142, 185)"><a href="https://vnkienthuc.com/threads/tim-hieu-ve-lop-stack-trong-c.77362/" target="_blank"><span style="font-size: 18px">Custom Attribute.</span></a></span></p><p><span style="font-size: 18px"></span></p><p><span style="font-size: 18px"><span style="color: #0000ff"><strong>1. Xác định một Attribute trong C#</strong></span></span></p><p><span style="font-size: 18px"></span></p><p><span style="font-size: 18px"><span style="color: #000000">Cú pháp để xác định một Attribute trong C# như sau:</span></span></p><p><span style="font-size: 18px"></span></p><p><span style="font-size: 18px"><em><span style="color: #5900b3">[attribute(positional_parameter, name_parameter = giá_trị, ...)]</span></em></span></p><p><span style="font-size: 18px"><em><span style="color: #5900b3">element</span></em></span></p><p><span style="font-size: 18px"></span></p><p><span style="font-size: 18px"><span style="color: #000000">Tên của Attribute và giá trị của nó được xác định bên trong dấu ngoặc vuông, ở trước phần tử từ đó thuộc tính được áp dụng cho. <em>positional_parameter</em> xác định thông tin thiết yếu và <em>name_parameter</em> xác định thông tin tùy ý.</span></span></p><p><span style="font-size: 18px"></span></p><p><span style="font-size: 18px"><span style="color: #0000ff"><strong>2. Attribute được định nghĩa trước trong C#</strong></span></span></p><p><span style="font-size: 18px"></span></p><p><span style="font-size: 18px"><span style="color: #000000">.Net Framework cung cấp 3 Attribute được định nghĩa trước:</span></span></p><ul> <li data-xf-list-type="ul"><span style="font-size: 18px"><span style="color: #000000">AttributeUsage</span></span></li> <li data-xf-list-type="ul"><span style="font-size: 18px"><span style="color: #000000">Conditional</span></span></li> <li data-xf-list-type="ul"><span style="font-size: 18px"><span style="color: #000000">Obsolete</span></span></li> </ul><p><span style="font-size: 18px"><span style="color: #0000ff"><strong>3. AttributeUsage trong C#</strong></span></span></p><p><span style="font-size: 18px"></span></p><p><span style="font-size: 18px"><span style="color: #000000">Attribute được định nghĩa trước </span></span><a href="https://vnkienthuc.com/forums/cong-nghe-thong-tin.375/" target="_blank"><span style="font-size: 18px"><span style="color: rgb(61, 142, 185)">AttributeUsage</span> </span></a><span style="font-size: 18px"><span style="color: #000000">miêu tả cách một lớp custom Attribute có thể được sử dụng. Nó xác định kiểu của các item, mà từ đó Attribute có thể áp dụng cho.</span></span></p><p><span style="font-size: 18px"></span></p><p><span style="font-size: 18px"><span style="color: #000000">Cú pháp để xác định Attribute này trong C# như sau:</span></span></p><p><span style="font-size: 18px"></span></p><p><span style="font-size: 18px"><em><span style="color: #5900b3">[AttributeUsage(</span></em></span></p><p><span style="font-size: 18px"><em><span style="color: #5900b3"> validon,</span></em></span></p><p><span style="font-size: 18px"><em><span style="color: #5900b3"> AllowMultiple=allowmultiple,</span></em></span></p><p><span style="font-size: 18px"><em><span style="color: #5900b3"> Inherited=inherited</span></em></span></p><p><span style="font-size: 18px"><em><span style="color: #5900b3">)]</span></em></span></p><p><span style="font-size: 18px"></span></p><p><span style="font-size: 18px"><span style="color: #000000">Tại đây:</span></span></p><ul> <li data-xf-list-type="ul"><span style="color: rgb(41, 105, 176)"><a href="https://vnkienthuc.com/forums/thu-thuat-tin-hoc.376/" target="_blank"><span style="font-size: 18px">Tham số <em>validon</em></span></a></span><span style="font-size: 18px"><span style="color: #000000"> xác định các phần tử ngôn ngữ mà Attribute có thể được đặt. Nó là một sự tổ hợp giá trị của một AttributeTargets enumerator. Giá trị mặc định là AttributeTargets.All.</span></span></li> <li data-xf-list-type="ul"><span style="font-size: 18px"><span style="color: #000000">Tham số <em>allowmultiple</em> (tùy ý) cung cấp giá trị cho thuộc tính AllowMultiple của attribute này, một giá trị Boolean. Nếu điều này là true, Attribute là multiuse. Giá trị mặc định là false (tức là single-use).</span></span></li> <li data-xf-list-type="ul"><span style="font-size: 18px"><span style="color: #000000">Tham số <em>inherited</em> (tùy ý) cung cấp giá trị cho thuộc tính Inherited của attribute này, một giá trị Boolean. Nếu nó là true, Attribute được kế thừa bởi các lớp kế thừa. Giá trị mặc định là false (không được kế thừa).</span></span></li> </ul><p><span style="font-size: 18px"><span style="color: #000000">Ví dụ:</span></span></p><p><span style="font-size: 18px"></span></p><p><span style="font-size: 18px"><em><span style="color: #5900b3">[AttributeUsage(AttributeTargets.Class |</span></em></span></p><p><span style="font-size: 18px"><em><span style="color: #5900b3">AttributeTargets.Constructor |</span></em></span></p><p><span style="font-size: 18px"><em><span style="color: #5900b3">AttributeTargets.Feild |</span></em></span></p><p><span style="font-size: 18px"><em><span style="color: #5900b3">AttributeTargets.Method |</span></em></span></p><p><span style="font-size: 18px"><em><span style="color: #5900b3">AttributeTargets.Property, </span></em></span></p><p><span style="font-size: 18px"><em><span style="color: #5900b3">AllowMultiple = true)]</span></em></span></p><p><span style="font-size: 18px"></span></p><p><span style="font-size: 18px"><span style="color: #0000ff"><strong>3. Conditional trong C#</strong></span></span></p><p><span style="font-size: 18px"></span></p><p><span style="font-size: 18px"><span style="color: #000000">Attribute tiền định nghĩa này đánh dấu một phương thức có điều kiện mà sự thực thi của nó phụ thuộc vào một tiến trình tiền xử lý định danh đã cho.</span></span></p><p><span style="font-size: 18px"></span></p><p><span style="font-size: 18px"><span style="color: #000000">Nó tạo sự biên dịch có điều kiện của các lời gọi phương thức, phụ thuộc vào giá trị đã cho, như Debug hoặcTrace. Ví dụ: nó hiển thị các giá trị của các biến trong khi debug một code.</span></span></p><p><span style="font-size: 18px"></span></p><p><span style="font-size: 18px"><span style="color: #000000">Cú pháp để </span></span><a href="https://vnkienthuc.com/threads/tim-hieu-ve-phuong-thuc-nac-danh-trong-c.77386/" target="_blank"><span style="font-size: 18px"><span style="color: rgb(61, 142, 185)">xác định Attribute</span> </span></a><span style="font-size: 18px"><span style="color: #000000">này trong C# là như sau:</span></span></p><p><span style="font-size: 18px"></span></p><p><span style="font-size: 18px"><em><span style="color: #5900b3">[Conditional(</span></em></span></p><p><span style="font-size: 18px"><em><span style="color: #5900b3"> conditionalSymbol</span></em></span></p><p><span style="font-size: 18px"><em><span style="color: #5900b3">)]</span></em></span></p><p><span style="font-size: 18px"></span></p><p><span style="font-size: 18px"><span style="color: #000000">Ví dụ: </span><span style="color: rgb(0, 0, 0)">[Conditional("DEBUG")]</span></span></p><p><span style="font-size: 18px"></span></p><p><span style="font-size: 18px"><span style="color: #000000">Sau đây là ví dụ minh họa Conditional trong C#: Tạo hai lớp có tên lần lượt là TestAttribute, TestScharp như sau:</span></span></p><p><span style="font-size: 18px"></span></p><p><span style="font-size: 18px"><span style="color: #000000">Lớp TestAttribute:</span></span></p><p><span style="font-size: 18px"></span></p><p><span style="font-size: 18px"><em><span style="color: #5900b3">#define DEBUG</span></em></span></p><p><span style="font-size: 18px"><em><span style="color: #5900b3">using System;</span></em></span></p><p><span style="font-size: 18px"><em><span style="color: #5900b3">using System.Diagnostics;</span></em></span></p><p><span style="font-size: 18px"></span></p><p><span style="font-size: 18px"><em><span style="color: #5900b3">namespace VietJackCsharp</span></em></span></p><p><span style="font-size: 18px"><em><span style="color: #5900b3">{</span></em></span></p><p><span style="font-size: 18px"><em><span style="color: #5900b3"> class TestAttribute</span></em></span></p><p><span style="font-size: 18px"><em><span style="color: #5900b3"> {</span></em></span></p><p><span style="font-size: 18px"><em><span style="color: #5900b3"> [Conditional("DEBUG")]</span></em></span></p><p><span style="font-size: 18px"><em><span style="color: #5900b3"> public static void Message(string msg)</span></em></span></p><p><span style="font-size: 18px"><em><span style="color: #5900b3"> {</span></em></span></p><p><span style="font-size: 18px"><em><span style="color: #5900b3"> Console.WriteLine(msg);</span></em></span></p><p><span style="font-size: 18px"><em><span style="color: #5900b3"> }</span></em></span></p><p><span style="font-size: 18px"><em><span style="color: #5900b3"> }</span></em></span></p><p><span style="font-size: 18px"><em><span style="color: #5900b3">}</span></em></span></p><p><span style="font-size: 18px"><em><span style="color: #5900b3">Lớp TestCsharp:</span></em></span></p><p><span style="font-size: 18px"></span></p><p><span style="font-size: 18px"><em><span style="color: #5900b3">using System;</span></em></span></p><p><span style="font-size: 18px"></span></p><p><span style="font-size: 18px"><em><span style="color: #5900b3">namespace VietJackCsharp</span></em></span></p><p><span style="font-size: 18px"><em><span style="color: #5900b3">{</span></em></span></p><p><span style="font-size: 18px"><em><span style="color: #5900b3"> class TestCsharp</span></em></span></p><p><span style="font-size: 18px"><em><span style="color: #5900b3"> {</span></em></span></p><p><span style="font-size: 18px"><em><span style="color: #5900b3"> static void function1()</span></em></span></p><p><span style="font-size: 18px"><em><span style="color: #5900b3"> {</span></em></span></p><p><span style="font-size: 18px"><em><span style="color: #5900b3"> TestAttribute.Message("Trong Function 1.");</span></em></span></p><p><span style="font-size: 18px"><em><span style="color: #5900b3"> function2();</span></em></span></p><p><span style="font-size: 18px"><em><span style="color: #5900b3"> }</span></em></span></p><p><span style="font-size: 18px"><em><span style="color: #5900b3"> static void function2()</span></em></span></p><p><span style="font-size: 18px"><em><span style="color: #5900b3"> {</span></em></span></p><p><span style="font-size: 18px"><em><span style="color: #5900b3"> TestAttribute.Message("Trong Function 2.");</span></em></span></p><p><span style="font-size: 18px"><em><span style="color: #5900b3"> }</span></em></span></p><p><span style="font-size: 18px"></span></p><p><span style="font-size: 18px"><em><span style="color: #5900b3"> public static void Main()</span></em></span></p><p><span style="font-size: 18px"><em><span style="color: #5900b3"> {</span></em></span></p><p><span style="font-size: 18px"><em><span style="color: #5900b3"> Console.WriteLine("Attribute trong C#");</span></em></span></p><p><span style="font-size: 18px"><em><span style="color: #5900b3"> Console.WriteLine("-----------------------");</span></em></span></p><p><span style="font-size: 18px"></span></p><p><span style="font-size: 18px"><em><span style="color: #5900b3"> TestAttribute.Message("Trong ham Main.");</span></em></span></p><p><span style="font-size: 18px"><em><span style="color: #5900b3"> function1();</span></em></span></p><p><span style="font-size: 18px"><em><span style="color: #5900b3"> Console.ReadKey();</span></em></span></p><p><span style="font-size: 18px"><em><span style="color: #5900b3"> }</span></em></span></p><p><span style="font-size: 18px"><em><span style="color: #5900b3"> }</span></em></span></p><p><span style="font-size: 18px"><em><span style="color: #5900b3">}</span></em></span></p><p><span style="font-size: 18px"></span></p><p><span style="font-size: 18px"><span style="color: #000000">Nếu bạn không sử dụng lệnh Console.ReadKey(); thì chương trình sẽ chạy và kết thúc luôn (nhanh quá đến nỗi bạn không kịp nhìn kết quả). Lệnh này cho phép chúng ta nhìn kết quả một cách rõ ràng hơn.</span></span></p><p><span style="font-size: 18px"></span></p><p><span style="font-size: 18px"><span style="color: #000000">Biên dịch và chạy chương trình C# trên sẽ cho kết quả sau:</span></span></p><p><span style="font-size: 18px"></span></p><p style="text-align: center"><span style="font-size: 18px"><span style="color: #000000"><img src="https://vietjack.com/csharp/images/attribute.PNG" alt="" class="fr-fic fr-dii fr-draggable " data-size="" style="" /></span></span></p><p><span style="font-size: 18px"></span></p><p><span style="font-size: 18px"><span style="color: #0000ff"><strong>4. Obsolete trong C#</strong></span></span></p><p><span style="font-size: 18px"></span></p><p><span style="font-size: 18px"><span style="color: #000000">Attribute tiền định nghĩa này trong C# đánh dấu một thực thể chương trình mà không nên được sử dụng. Nó cho bạn khả năng để thông báo cho compiler để loại bỏ một phần tử target cụ thể. Ví dụ, khi một phương thức mới đang được sử dụng trong một lớp và nếu bạn vẫn muốn giữ lại phương thức cũ trong lớp này, bạn có thể đánh dấu nó là obsolete bằng việc hiển thị một thông báo là phương thức mới nên được sử dụng, thay cho phương thức cũ.</span></span></p><p><span style="font-size: 18px"></span></p><p><span style="font-size: 18px"><span style="color: #000000">Cú pháp để xác định Attribute này trong C# là như sau:</span></span></p><p><span style="font-size: 18px"></span></p><p><span style="font-size: 18px"><em><span style="color: #5900b3">[Obsolete(</span></em></span></p><p><span style="font-size: 18px"><em><span style="color: #5900b3"> message</span></em></span></p><p><span style="font-size: 18px"><em><span style="color: #5900b3">)]</span></em></span></p><p><span style="font-size: 18px"><em><span style="color: #5900b3">[Obsolete(</span></em></span></p><p><span style="font-size: 18px"><em><span style="color: #5900b3"> message,</span></em></span></p><p><span style="font-size: 18px"><em><span style="color: #5900b3"> iserror</span></em></span></p><p><span style="font-size: 18px"><em><span style="color: #5900b3">)]</span></em></span></p><p><span style="font-size: 18px"></span></p><p><span style="font-size: 18px"><span style="color: #000000">Tại đây,</span></span></p><ul> <li data-xf-list-type="ul"><span style="font-size: 18px"><span style="color: #000000">Tham số <em>message</em> là một chuỗi miêu tả lý do tại sao item là obsolete và cái gì được sử dụng thay cho nó.</span></span></li> <li data-xf-list-type="ul"><span style="font-size: 18px"><span style="color: #000000">Tham số <em>iserror</em> là một giá trị Boolean. Nếu giá trị của nó là true, compiler nên đối xử sự sử dụng của item này như là một lỗi. Giá trị mặc định là false (tức là compiler tạo một warning).</span></span></li> </ul><p><span style="font-size: 18px"><span style="color: #000000">Ví dụ sau minh họa obsolete trong C#:</span></span></p><p><span style="font-size: 18px"></span></p><p><span style="font-size: 18px"><em><span style="color: #5900b3">using System;</span></em></span></p><p><span style="font-size: 18px"><em><span style="color: #5900b3">namespace VietJackCsharp</span></em></span></p><p><span style="font-size: 18px"><em><span style="color: #5900b3">{</span></em></span></p><p><span style="font-size: 18px"><em><span style="color: #5900b3"> class TestCsharp</span></em></span></p><p><span style="font-size: 18px"><em><span style="color: #5900b3"> {</span></em></span></p><p><span style="font-size: 18px"><em><span style="color: #5900b3"> [Obsolete("Dung su dung PhuongThuc1, ban nen su dung PhuongThuc2", true)]</span></em></span></p><p><span style="font-size: 18px"><em><span style="color: #5900b3"> static void PhuongThuc1()</span></em></span></p><p><span style="font-size: 18px"><em><span style="color: #5900b3"> {</span></em></span></p><p><span style="font-size: 18px"><em><span style="color: #5900b3"> Console.WriteLine("Day la phuong thuc 1.");</span></em></span></p><p><span style="font-size: 18px"><em><span style="color: #5900b3"> }</span></em></span></p><p><span style="font-size: 18px"><em><span style="color: #5900b3"> static void PhuongThuc2()</span></em></span></p><p><span style="font-size: 18px"><em><span style="color: #5900b3"> {</span></em></span></p><p><span style="font-size: 18px"><em><span style="color: #5900b3"> Console.WriteLine("Day la phuong thuc 2.");</span></em></span></p><p><span style="font-size: 18px"><em><span style="color: #5900b3"> }</span></em></span></p><p><span style="font-size: 18px"><em><span style="color: #5900b3"> public static void Main()</span></em></span></p><p><span style="font-size: 18px"><em><span style="color: #5900b3"> {</span></em></span></p><p><span style="font-size: 18px"><em><span style="color: #5900b3"> PhuongThuc1(); // co dau gach do bao hieu tai day</span></em></span></p><p><span style="font-size: 18px"><em><span style="color: #5900b3"> }</span></em></span></p><p><span style="font-size: 18px"><em><span style="color: #5900b3"> }</span></em></span></p><p><span style="font-size: 18px"><em><span style="color: #5900b3">}</span></em></span></p><p><span style="font-size: 18px"><span style="color: #000000">Khi đó, ở lời gọi PhuongThuc1() trong hàm main() sẽ xuất hiện dấu gạch đỏ, nếu bạn rê chuột qua sẽ có một thông báo có dạng:</span></span></p><p><span style="font-size: 18px"></span></p><p><span style="font-size: 18px"><span style="color: #000000">'VietJackCsharp.TestCsharp.PhuongThuc1()' is obsolete: 'Dung su dung PhuongThuc1, ban nen su dung PhuongThuc2'</span></span></p><p><span style="font-size: 18px"></span></p><p><span style="font-size: 18px"><span style="color: #0000ff"><strong>5. Tạo Custom Attribute trong C#</strong></span></span></p><p><span style="font-size: 18px"></span></p><p><span style="font-size: 18px"><span style="color: #000000">Còn gọi là Attribute tùy biến hay Attribute do người dùng tự định nghĩa. .Net Framework cho phép tạo các Custom Attribute mà có thể được sử dụng để lưu giữ thông tin tường thuật và có thể được thu nhận tại runtime. Thông tin này có thể liên quan tới bất kỳ phần tử target nào phụ thuộc vào chuẩn thiết kế và yêu cầu ứng dụng.</span></span></p><p><span style="font-size: 18px"></span></p><p><span style="font-size: 18px"><span style="color: #000000">Tạo và sử dụng Custom Attribute trong C# bao gồm 4 bước sau:</span></span></p><ul> <li data-xf-list-type="ul"><span style="font-size: 18px"><span style="color: #000000">Khai báo một Custom Attribute</span></span></li> <li data-xf-list-type="ul"><span style="font-size: 18px"><span style="color: #000000">Xây dựng Custom Attribute</span></span></li> <li data-xf-list-type="ul"><span style="font-size: 18px"><span style="color: #000000">Áp dụng Attribute trên một phần tử chương trình target</span></span></li> <li data-xf-list-type="ul"><span style="font-size: 18px"><span style="color: #000000">Truy cập các Attribute thông qua Reflection</span></span></li> </ul><p><span style="font-size: 18px"><span style="color: #000000">Bước cuối cùng liên quan tới việc viết một chương trình đơn giản để đọc qua metadata để tìm ra các notation đa dạng. Metadata là dữ liệu hoặc thông tin được sử dụng để miêu tả dữ liệu khác. Chương trình này nên sử dụng các Reflection để truy cập các Attribute tại runtime. Chúng ta sẽ bàn luận điều này trong chương tới.</span></span></p><p><span style="font-size: 18px"></span></p><p><span style="font-size: 18px"><span style="color: #0000ff"><strong>6. Khai báo một Custom Attribute trong C#</strong></span></span></p><p><span style="font-size: 18px"></span></p><p><span style="font-size: 18px"><span style="color: #000000">Một Custom Attribute mới nên được kế thừa từ lớp System.Attribute trong C#. Ví dụ:</span></span></p><p><span style="font-size: 18px"></span></p><p><span style="font-size: 18px"><em><span style="color: #5900b3">//a custom attribute BugFix to be assigned to a class and its members</span></em></span></p><p><span style="font-size: 18px"><em><span style="color: #5900b3">[AttributeUsage(AttributeTargets.Class |</span></em></span></p><p><span style="font-size: 18px"><em><span style="color: #5900b3">AttributeTargets.Constructor |</span></em></span></p><p><span style="font-size: 18px"><em><span style="color: #5900b3">AttributeTargets.Field |</span></em></span></p><p><span style="font-size: 18px"><em><span style="color: #5900b3">AttributeTargets.Method |</span></em></span></p><p><span style="font-size: 18px"><em><span style="color: #5900b3">AttributeTargets.Property,</span></em></span></p><p><span style="font-size: 18px"><em><span style="color: #5900b3">AllowMultiple = true)]</span></em></span></p><p><span style="font-size: 18px"></span></p><p><span style="font-size: 18px"><span style="color: #000000">public class DeBugInfo : System.Attribute</span></span></p><p><span style="font-size: 18px"><span style="color: #000000">Trong code trên, chúng ta đã khai báo một Custom Attribute là <em>DeBugInfo</em>.</span></span></p><p><span style="font-size: 18px"></span></p><p><span style="font-size: 18px"><span style="color: #0000ff"><strong>7. Xây dựng Custom Attribute trong C#</strong></span></span></p><p><span style="font-size: 18px"></span></p><p><span style="font-size: 18px"><span style="color: #000000">Chúng ta cùng xây dựng Custom Attribute có tên là <em>DeBugInfo</em>, mà lưu giữ thông tin thu được bởi việc debug bất kỳ chương trình nào. Nó có thể giữ thông tin sau:</span></span></p><ul> <li data-xf-list-type="ul"><span style="font-size: 18px"><span style="color: #000000">Số hiệu code để bug</span></span></li> <li data-xf-list-type="ul"><span style="font-size: 18px"><span style="color: #000000">Tên lập trình viên, người nhận diện bug đó</span></span></li> <li data-xf-list-type="ul"><span style="font-size: 18px"><span style="color: #000000">Ngày review cuối cùng của code đó</span></span></li> <li data-xf-list-type="ul"><span style="font-size: 18px"><span style="color: #000000">Một thông báo dạng chuỗi để lưu giữ các lưu ý của lập trình viên</span></span></li> </ul><p><span style="font-size: 18px"><span style="color: #000000">Lớp <em>DeBugInfo</em> có 3 thuộc tính private để lưu giữ 3 thông tin đầu tiên và một 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, và ngày review là các tham số vị trí tương ứng của lớp DeBugInfo và thông báo là một tham số tùy ý.</span></span></p><p><span style="font-size: 18px"></span></p><p><span style="font-size: 18px"><span style="color: #000000">Mỗi Attribute phải có ít nhất một constructor. Các tham số vị trí tương ứng nên được truyền thông qua constructor đó. Ví dụ sau minh họa lớp <em>DeBugInfo</em> trên:</span></span></p><p><span style="font-size: 18px"></span></p><p><span style="font-size: 18px"><span style="color: #000000">//Vi du minh hoa mot custom attribute BugFix</span></span></p><p><span style="font-size: 18px"></span></p><p><span style="font-size: 18px"><em><span style="color: #5900b3">[AttributeUsage(AttributeTargets.Class |</span></em></span></p><p><span style="font-size: 18px"><em><span style="color: #5900b3">AttributeTargets.Constructor |</span></em></span></p><p><span style="font-size: 18px"><em><span style="color: #5900b3">AttributeTargets.Field |</span></em></span></p><p><span style="font-size: 18px"><em><span style="color: #5900b3">AttributeTargets.Method |</span></em></span></p><p><span style="font-size: 18px"><em><span style="color: #5900b3">AttributeTargets.Property,</span></em></span></p><p><span style="font-size: 18px"><em><span style="color: #5900b3">AllowMultiple = true)]</span></em></span></p><p><span style="font-size: 18px"></span></p><p><span style="font-size: 18px"><em><span style="color: #5900b3">public class DeBugInfo : System.Attribute</span></em></span></p><p><span style="font-size: 18px"><em><span style="color: #5900b3">{</span></em></span></p><p><span style="font-size: 18px"><em><span style="color: #5900b3"> private int bugNo;</span></em></span></p><p><span style="font-size: 18px"><em><span style="color: #5900b3"> private string developer;</span></em></span></p><p><span style="font-size: 18px"><em><span style="color: #5900b3"> private string lastReview;</span></em></span></p><p><span style="font-size: 18px"><em><span style="color: #5900b3"> public string message;</span></em></span></p><p><span style="font-size: 18px"><em><span style="color: #5900b3"> </span></em></span></p><p><span style="font-size: 18px"><em><span style="color: #5900b3"> public DeBugInfo(int bg, string dev, string d)</span></em></span></p><p><span style="font-size: 18px"><em><span style="color: #5900b3"> {</span></em></span></p><p><span style="font-size: 18px"><em><span style="color: #5900b3"> this.bugNo = bg;</span></em></span></p><p><span style="font-size: 18px"><em><span style="color: #5900b3"> this.developer = dev;</span></em></span></p><p><span style="font-size: 18px"><em><span style="color: #5900b3"> this.lastReview = d;</span></em></span></p><p><span style="font-size: 18px"><em><span style="color: #5900b3"> }</span></em></span></p><p><span style="font-size: 18px"><em><span style="color: #5900b3"> </span></em></span></p><p><span style="font-size: 18px"><em><span style="color: #5900b3"> public int BugNo</span></em></span></p><p><span style="font-size: 18px"><em><span style="color: #5900b3"> {</span></em></span></p><p><span style="font-size: 18px"><em><span style="color: #5900b3"> get</span></em></span></p><p><span style="font-size: 18px"><em><span style="color: #5900b3"> {</span></em></span></p><p><span style="font-size: 18px"><em><span style="color: #5900b3"> return bugNo;</span></em></span></p><p><span style="font-size: 18px"><em><span style="color: #5900b3"> }</span></em></span></p><p><span style="font-size: 18px"><em><span style="color: #5900b3"> }</span></em></span></p><p><span style="font-size: 18px"><em><span style="color: #5900b3"> </span></em></span></p><p><span style="font-size: 18px"><em><span style="color: #5900b3"> public string Developer</span></em></span></p><p><span style="font-size: 18px"><em><span style="color: #5900b3"> {</span></em></span></p><p><span style="font-size: 18px"><em><span style="color: #5900b3"> get</span></em></span></p><p><span style="font-size: 18px"><em><span style="color: #5900b3"> {</span></em></span></p><p><span style="font-size: 18px"><em><span style="color: #5900b3"> return developer;</span></em></span></p><p><span style="font-size: 18px"><em><span style="color: #5900b3"> }</span></em></span></p><p><span style="font-size: 18px"><em><span style="color: #5900b3"> }</span></em></span></p><p><span style="font-size: 18px"><em><span style="color: #5900b3"> </span></em></span></p><p><span style="font-size: 18px"><em><span style="color: #5900b3"> public string LastReview</span></em></span></p><p><span style="font-size: 18px"><em><span style="color: #5900b3"> {</span></em></span></p><p><span style="font-size: 18px"><em><span style="color: #5900b3"> get</span></em></span></p><p><span style="font-size: 18px"><em><span style="color: #5900b3"> {</span></em></span></p><p><span style="font-size: 18px"><em><span style="color: #5900b3"> return lastReview;</span></em></span></p><p><span style="font-size: 18px"><em><span style="color: #5900b3"> }</span></em></span></p><p><span style="font-size: 18px"><em><span style="color: #5900b3"> }</span></em></span></p><p><span style="font-size: 18px"><em><span style="color: #5900b3"> </span></em></span></p><p><span style="font-size: 18px"><em><span style="color: #5900b3"> public string Message</span></em></span></p><p><span style="font-size: 18px"><em><span style="color: #5900b3"> {</span></em></span></p><p><span style="font-size: 18px"><em><span style="color: #5900b3"> get</span></em></span></p><p><span style="font-size: 18px"><em><span style="color: #5900b3"> {</span></em></span></p><p><span style="font-size: 18px"><em><span style="color: #5900b3"> return message;</span></em></span></p><p><span style="font-size: 18px"><em><span style="color: #5900b3"> }</span></em></span></p><p><span style="font-size: 18px"><em><span style="color: #5900b3"> set</span></em></span></p><p><span style="font-size: 18px"><em><span style="color: #5900b3"> {</span></em></span></p><p><span style="font-size: 18px"><em><span style="color: #5900b3"> message = value;</span></em></span></p><p><span style="font-size: 18px"><em><span style="color: #5900b3"> }</span></em></span></p><p><span style="font-size: 18px"><em><span style="color: #5900b3"> }</span></em></span></p><p><span style="font-size: 18px"><em><span style="color: #5900b3">}</span></em></span></p><p><span style="font-size: 18px"><span style="color: #0000ff"><strong>8. Áp dụng Custom Attribute trong C#</strong></span></span></p><p><span style="font-size: 18px"></span></p><p><span style="font-size: 18px"><span style="color: #000000">Custom Attribute trong C# được áp dụng bằng việc đặt nó ngay trước target của nó:</span></span></p><p><span style="font-size: 18px"></span></p><p><span style="font-size: 18px"><em><span style="color: #5900b3">[DeBugInfo(45, "Tran Nam", "2/8/2016", Message = "Kieu tra ve khong hop le")]</span></em></span></p><p><span style="font-size: 18px"><em><span style="color: #5900b3">[DeBugInfo(49, "Minh Chinh", "10/10/2016", Message = "Bien chua duoc su dung")]</span></em></span></p><p><span style="font-size: 18px"><em><span style="color: #5900b3">class Rectangle</span></em></span></p><p><span style="font-size: 18px"><em><span style="color: #5900b3">{</span></em></span></p><p><span style="font-size: 18px"><em><span style="color: #5900b3"> //cac bien thanh vien</span></em></span></p><p><span style="font-size: 18px"><em><span style="color: #5900b3"> protected double chieu_dai;</span></em></span></p><p><span style="font-size: 18px"><em><span style="color: #5900b3"> protected double chieu_rong;</span></em></span></p><p><span style="font-size: 18px"><em><span style="color: #5900b3"> public Rectangle(double l, double w)</span></em></span></p><p><span style="font-size: 18px"><em><span style="color: #5900b3"> {</span></em></span></p><p><span style="font-size: 18px"><em><span style="color: #5900b3"> chieu_dai = l;</span></em></span></p><p><span style="font-size: 18px"><em><span style="color: #5900b3"> chieu_rong = w;</span></em></span></p><p><span style="font-size: 18px"><em><span style="color: #5900b3"> }</span></em></span></p><p><span style="font-size: 18px"><em><span style="color: #5900b3"> [DeBugInfo(55, "Tran Nam", "2/8/2016", Message = "Kieu tra ve khong hop le")]</span></em></span></p><p><span style="font-size: 18px"><em><span style="color: #5900b3"> </span></em></span></p><p><span style="font-size: 18px"><em><span style="color: #5900b3"> public double tinhDienTich()</span></em></span></p><p><span style="font-size: 18px"><em><span style="color: #5900b3"> {</span></em></span></p><p><span style="font-size: 18px"><em><span style="color: #5900b3"> return chieu_dai * chieu_rong;</span></em></span></p><p><span style="font-size: 18px"><em><span style="color: #5900b3"> }</span></em></span></p><p><span style="font-size: 18px"><em><span style="color: #5900b3"> [DeBugInfo(56, "Minh Chinh", "10/10/2016")]</span></em></span></p><p><span style="font-size: 18px"><em><span style="color: #5900b3"> </span></em></span></p><p><span style="font-size: 18px"><em><span style="color: #5900b3"> public void Display()</span></em></span></p><p><span style="font-size: 18px"><em><span style="color: #5900b3"> {</span></em></span></p><p><span style="font-size: 18px"><em><span style="color: #5900b3"> Console.WriteLine("Chieu dai: {0}", chieu_dai);</span></em></span></p><p><span style="font-size: 18px"><em><span style="color: #5900b3"> Console.WriteLine("Chieu rong: {0}", chieu_rong);</span></em></span></p><p><span style="font-size: 18px"><em><span style="color: #5900b3"> Console.WriteLine("Dien tich: {0}", tinhDienTich());</span></em></span></p><p><span style="font-size: 18px"><em><span style="color: #5900b3"> }</span></em></span></p><p><span style="font-size: 18px"><em><span style="color: #5900b3">}</span></em></span></p><p><span style="font-size: 18px"></span></p><p><span style="font-size: 18px"><span style="color: #000000">Trên đây là bài giới thiệu về attribute trong C# và cách sử dụng nó. Chúc các bạn một ngày học tập vui vẻ, hiệu quả.</span></span></p><p><span style="font-size: 18px"></span></p><p style="text-align: right"><span style="font-size: 18px"><span style="color: rgb(0, 0, 0)">Nguồn: vietjack.com</span></span></p></blockquote><p></p>
[QUOTE="uocmo_kchodoi, post: 177596, member: 165510"] [B][SIZE=5][COLOR=rgb(0, 0, 0)] [/COLOR][/SIZE][/B][SIZE=5][COLOR=rgb(0, 0, 0)]Attribute là một cơ chế được dùng để định nghĩa và khai báo các thông tin cần thiết, nhằm bổ sung và hỗ trợ khá nhiều chức năng liên quan đến việc soạn thảo, debug, cũng như biên dịch các chương trình.[/COLOR] [/SIZE] [COLOR=rgb(41, 105, 176)][URL='https://vnkienthuc.com/forums/code.377/'][SIZE=5]Một attribute trong C#[/SIZE][/URL][/COLOR][SIZE=5][COLOR=#000000], là một thẻ tường thuật, được sử dụng để truyền thông tin tới runtime về các hành vi của các phần tử đa dạng như các lớp, phương thức, cấu trúc, enum, assembly, … trong chương trình của bạn. Bạn có thể thêm thông tin tường thuật tới một chương trình bởi việc sử dụng một Attribute. Một thẻ tường thuật được miêu tả bởi các dấu ngoặc móc vuông ([]) được đặt bên trên phần tử mà nó được sử dụng cho. Nhiều attribute có thể được sử dụng trong cùng một cặp ngoặc vuông này và được ngăn cách nhau bởi dấu phẩy “,”.[/COLOR] [/SIZE] [COLOR=rgb(61, 142, 185)][URL='https://vnkienthuc.com/threads/tim-hieu-ve-reflection-trong-c.77353/'][SIZE=5]Các Attribute[/SIZE][/URL][/COLOR][SIZE=5][COLOR=rgb(61, 142, 185)] [/COLOR][COLOR=#000000]được sử dụng để thêm metadata, ví dụ như chỉ lệnh biên dịch và thông tin khác như comment, miêu tả, phương thức và các lớp tới một chương trình. .Net Framework cung cấp hai kiểu Attribute: các Attribute được định nghĩa trước và các [/COLOR][/SIZE][COLOR=rgb(61, 142, 185)][URL='https://vnkienthuc.com/threads/tim-hieu-ve-lop-stack-trong-c.77362/'][SIZE=5]Custom Attribute.[/SIZE][/URL][/COLOR] [SIZE=5] [COLOR=#0000ff][B]1. Xác định một Attribute trong C#[/B][/COLOR] [COLOR=#000000]Cú pháp để xác định một Attribute trong C# như sau:[/COLOR] [I][COLOR=#5900b3][attribute(positional_parameter, name_parameter = giá_trị, ...)][/COLOR][/I] [I][COLOR=#5900b3]element[/COLOR][/I] [COLOR=#000000]Tên của Attribute và giá trị của nó được xác định bên trong dấu ngoặc vuông, ở trước phần tử từ đó thuộc tính được áp dụng cho. [I]positional_parameter[/I] xác định thông tin thiết yếu và [I]name_parameter[/I] xác định thông tin tùy ý.[/COLOR] [COLOR=#0000ff][B]2. Attribute được định nghĩa trước trong C#[/B][/COLOR] [COLOR=#000000].Net Framework cung cấp 3 Attribute được định nghĩa trước:[/COLOR][/SIZE] [LIST] [*][SIZE=5][COLOR=#000000]AttributeUsage[/COLOR][/SIZE] [*][SIZE=5][COLOR=#000000]Conditional[/COLOR][/SIZE] [*][SIZE=5][COLOR=#000000]Obsolete[/COLOR][/SIZE] [/LIST] [SIZE=5][COLOR=#0000ff][B]3. AttributeUsage trong C#[/B][/COLOR] [COLOR=#000000]Attribute được định nghĩa trước [/COLOR][/SIZE][URL='https://vnkienthuc.com/forums/cong-nghe-thong-tin.375/'][SIZE=5][COLOR=rgb(61, 142, 185)]AttributeUsage[/COLOR][COLOR=#000000] [/COLOR][/SIZE][/URL][SIZE=5][COLOR=#000000]miêu tả cách một lớp custom Attribute có thể được sử dụng. Nó xác định kiểu của các item, mà từ đó Attribute có thể áp dụng cho.[/COLOR] [COLOR=#000000]Cú pháp để xác định Attribute này trong C# như sau:[/COLOR] [I][COLOR=#5900b3][AttributeUsage([/COLOR][/I] [I][COLOR=#5900b3] validon,[/COLOR][/I] [I][COLOR=#5900b3] AllowMultiple=allowmultiple,[/COLOR][/I] [I][COLOR=#5900b3] Inherited=inherited[/COLOR][/I] [I][COLOR=#5900b3])][/COLOR][/I] [COLOR=#000000]Tại đây:[/COLOR][/SIZE] [LIST] [*][COLOR=rgb(41, 105, 176)][URL='https://vnkienthuc.com/forums/thu-thuat-tin-hoc.376/'][SIZE=5]Tham số [I]validon[/I][/SIZE][/URL][/COLOR][SIZE=5][COLOR=#000000] xác định các phần tử ngôn ngữ mà Attribute có thể được đặt. Nó là một sự tổ hợp giá trị của một AttributeTargets enumerator. Giá trị mặc định là AttributeTargets.All.[/COLOR][/SIZE] [*][SIZE=5][COLOR=#000000]Tham số [I]allowmultiple[/I] (tùy ý) cung cấp giá trị cho thuộc tính AllowMultiple của attribute này, một giá trị Boolean. Nếu điều này là true, Attribute là multiuse. Giá trị mặc định là false (tức là single-use).[/COLOR][/SIZE] [*][SIZE=5][COLOR=#000000]Tham số [I]inherited[/I] (tùy ý) cung cấp giá trị cho thuộc tính Inherited của attribute này, một giá trị Boolean. Nếu nó là true, Attribute được kế thừa bởi các lớp kế thừa. Giá trị mặc định là false (không được kế thừa).[/COLOR][/SIZE] [/LIST] [SIZE=5][COLOR=#000000]Ví dụ:[/COLOR] [I][COLOR=#5900b3][AttributeUsage(AttributeTargets.Class |[/COLOR][/I] [I][COLOR=#5900b3]AttributeTargets.Constructor |[/COLOR][/I] [I][COLOR=#5900b3]AttributeTargets.Feild |[/COLOR][/I] [I][COLOR=#5900b3]AttributeTargets.Method |[/COLOR][/I] [I][COLOR=#5900b3]AttributeTargets.Property, [/COLOR][/I] [I][COLOR=#5900b3]AllowMultiple = true)][/COLOR][/I] [COLOR=#0000ff][B]3. Conditional trong C#[/B][/COLOR] [COLOR=#000000]Attribute tiền định nghĩa này đánh dấu một phương thức có điều kiện mà sự thực thi của nó phụ thuộc vào một tiến trình tiền xử lý định danh đã cho.[/COLOR] [COLOR=#000000]Nó tạo sự biên dịch có điều kiện của các lời gọi phương thức, phụ thuộc vào giá trị đã cho, như Debug hoặcTrace. Ví dụ: nó hiển thị các giá trị của các biến trong khi debug một code.[/COLOR] [COLOR=#000000]Cú pháp để [/COLOR][/SIZE][URL='https://vnkienthuc.com/threads/tim-hieu-ve-phuong-thuc-nac-danh-trong-c.77386/'][SIZE=5][COLOR=rgb(61, 142, 185)]xác định Attribute[/COLOR][COLOR=#000000] [/COLOR][/SIZE][/URL][SIZE=5][COLOR=#000000]này trong C# là như sau:[/COLOR] [I][COLOR=#5900b3][Conditional([/COLOR][/I] [I][COLOR=#5900b3] conditionalSymbol[/COLOR][/I] [I][COLOR=#5900b3])][/COLOR][/I] [COLOR=#000000]Ví dụ: [/COLOR][COLOR=rgb(0, 0, 0)][Conditional("DEBUG")][/COLOR] [COLOR=#000000]Sau đây là ví dụ minh họa Conditional trong C#: Tạo hai lớp có tên lần lượt là TestAttribute, TestScharp như sau:[/COLOR] [COLOR=#000000]Lớp TestAttribute:[/COLOR] [I][COLOR=#5900b3]#define DEBUG[/COLOR][/I] [I][COLOR=#5900b3]using System;[/COLOR][/I] [I][COLOR=#5900b3]using System.Diagnostics;[/COLOR][/I] [I][COLOR=#5900b3]namespace VietJackCsharp[/COLOR][/I] [I][COLOR=#5900b3]{[/COLOR][/I] [I][COLOR=#5900b3] class TestAttribute[/COLOR][/I] [I][COLOR=#5900b3] {[/COLOR][/I] [I][COLOR=#5900b3] [Conditional("DEBUG")][/COLOR][/I] [I][COLOR=#5900b3] public static void Message(string msg)[/COLOR][/I] [I][COLOR=#5900b3] {[/COLOR][/I] [I][COLOR=#5900b3] Console.WriteLine(msg);[/COLOR][/I] [I][COLOR=#5900b3] }[/COLOR][/I] [I][COLOR=#5900b3] }[/COLOR][/I] [I][COLOR=#5900b3]}[/COLOR][/I] [I][COLOR=#5900b3]Lớp TestCsharp:[/COLOR][/I] [I][COLOR=#5900b3]using System;[/COLOR][/I] [I][COLOR=#5900b3]namespace VietJackCsharp[/COLOR][/I] [I][COLOR=#5900b3]{[/COLOR][/I] [I][COLOR=#5900b3] class TestCsharp[/COLOR][/I] [I][COLOR=#5900b3] {[/COLOR][/I] [I][COLOR=#5900b3] static void function1()[/COLOR][/I] [I][COLOR=#5900b3] {[/COLOR][/I] [I][COLOR=#5900b3] TestAttribute.Message("Trong Function 1.");[/COLOR][/I] [I][COLOR=#5900b3] function2();[/COLOR][/I] [I][COLOR=#5900b3] }[/COLOR][/I] [I][COLOR=#5900b3] static void function2()[/COLOR][/I] [I][COLOR=#5900b3] {[/COLOR][/I] [I][COLOR=#5900b3] TestAttribute.Message("Trong Function 2.");[/COLOR][/I] [I][COLOR=#5900b3] }[/COLOR][/I] [I][COLOR=#5900b3] public static void Main()[/COLOR][/I] [I][COLOR=#5900b3] {[/COLOR][/I] [I][COLOR=#5900b3] Console.WriteLine("Attribute trong C#");[/COLOR][/I] [I][COLOR=#5900b3] Console.WriteLine("-----------------------");[/COLOR][/I] [I][COLOR=#5900b3] TestAttribute.Message("Trong ham Main.");[/COLOR][/I] [I][COLOR=#5900b3] function1();[/COLOR][/I] [I][COLOR=#5900b3] Console.ReadKey();[/COLOR][/I] [I][COLOR=#5900b3] }[/COLOR][/I] [I][COLOR=#5900b3] }[/COLOR][/I] [I][COLOR=#5900b3]}[/COLOR][/I] [COLOR=#000000]Nếu bạn không sử dụng lệnh Console.ReadKey(); thì chương trình sẽ chạy và kết thúc luôn (nhanh quá đến nỗi bạn không kịp nhìn kết quả). Lệnh này cho phép chúng ta nhìn kết quả một cách rõ ràng hơn.[/COLOR] [COLOR=#000000]Biên dịch và chạy chương trình C# trên sẽ cho kết quả sau:[/COLOR] [/SIZE] [CENTER][SIZE=5][COLOR=#000000][IMG]https://vietjack.com/csharp/images/attribute.PNG[/IMG][/COLOR][/SIZE][/CENTER] [SIZE=5] [COLOR=#0000ff][B]4. Obsolete trong C#[/B][/COLOR] [COLOR=#000000]Attribute tiền định nghĩa này trong C# đánh dấu một thực thể chương trình mà không nên được sử dụng. Nó cho bạn khả năng để thông báo cho compiler để loại bỏ một phần tử target cụ thể. Ví dụ, khi một phương thức mới đang được sử dụng trong một lớp và nếu bạn vẫn muốn giữ lại phương thức cũ trong lớp này, bạn có thể đánh dấu nó là obsolete bằng việc hiển thị một thông báo là phương thức mới nên được sử dụng, thay cho phương thức cũ.[/COLOR] [COLOR=#000000]Cú pháp để xác định Attribute này trong C# là như sau:[/COLOR] [I][COLOR=#5900b3][Obsolete([/COLOR][/I] [I][COLOR=#5900b3] message[/COLOR][/I] [I][COLOR=#5900b3])][/COLOR][/I] [I][COLOR=#5900b3][Obsolete([/COLOR][/I] [I][COLOR=#5900b3] message,[/COLOR][/I] [I][COLOR=#5900b3] iserror[/COLOR][/I] [I][COLOR=#5900b3])][/COLOR][/I] [COLOR=#000000]Tại đây,[/COLOR][/SIZE] [LIST] [*][SIZE=5][COLOR=#000000]Tham số [I]message[/I] là một chuỗi miêu tả lý do tại sao item là obsolete và cái gì được sử dụng thay cho nó.[/COLOR][/SIZE] [*][SIZE=5][COLOR=#000000]Tham số [I]iserror[/I] là một giá trị Boolean. Nếu giá trị của nó là true, compiler nên đối xử sự sử dụng của item này như là một lỗi. Giá trị mặc định là false (tức là compiler tạo một warning).[/COLOR][/SIZE] [/LIST] [SIZE=5][COLOR=#000000]Ví dụ sau minh họa obsolete trong C#:[/COLOR] [I][COLOR=#5900b3]using System;[/COLOR][/I] [I][COLOR=#5900b3]namespace VietJackCsharp[/COLOR][/I] [I][COLOR=#5900b3]{[/COLOR][/I] [I][COLOR=#5900b3] class TestCsharp[/COLOR][/I] [I][COLOR=#5900b3] {[/COLOR][/I] [I][COLOR=#5900b3] [Obsolete("Dung su dung PhuongThuc1, ban nen su dung PhuongThuc2", true)][/COLOR][/I] [I][COLOR=#5900b3] static void PhuongThuc1()[/COLOR][/I] [I][COLOR=#5900b3] {[/COLOR][/I] [I][COLOR=#5900b3] Console.WriteLine("Day la phuong thuc 1.");[/COLOR][/I] [I][COLOR=#5900b3] }[/COLOR][/I] [I][COLOR=#5900b3] static void PhuongThuc2()[/COLOR][/I] [I][COLOR=#5900b3] {[/COLOR][/I] [I][COLOR=#5900b3] Console.WriteLine("Day la phuong thuc 2.");[/COLOR][/I] [I][COLOR=#5900b3] }[/COLOR][/I] [I][COLOR=#5900b3] public static void Main()[/COLOR][/I] [I][COLOR=#5900b3] {[/COLOR][/I] [I][COLOR=#5900b3] PhuongThuc1(); // co dau gach do bao hieu tai day[/COLOR][/I] [I][COLOR=#5900b3] }[/COLOR][/I] [I][COLOR=#5900b3] }[/COLOR][/I] [I][COLOR=#5900b3]}[/COLOR][/I] [COLOR=#000000]Khi đó, ở lời gọi PhuongThuc1() trong hàm main() sẽ xuất hiện dấu gạch đỏ, nếu bạn rê chuột qua sẽ có một thông báo có dạng:[/COLOR] [COLOR=#000000]'VietJackCsharp.TestCsharp.PhuongThuc1()' is obsolete: 'Dung su dung PhuongThuc1, ban nen su dung PhuongThuc2'[/COLOR] [COLOR=#0000ff][B]5. Tạo Custom Attribute trong C#[/B][/COLOR] [COLOR=#000000]Còn gọi là Attribute tùy biến hay Attribute do người dùng tự định nghĩa. .Net Framework cho phép tạo các Custom Attribute mà có thể được sử dụng để lưu giữ thông tin tường thuật và có thể được thu nhận tại runtime. Thông tin này có thể liên quan tới bất kỳ phần tử target nào phụ thuộc vào chuẩn thiết kế và yêu cầu ứng dụng.[/COLOR] [COLOR=#000000]Tạo và sử dụng Custom Attribute trong C# bao gồm 4 bước sau:[/COLOR][/SIZE] [LIST] [*][SIZE=5][COLOR=#000000]Khai báo một Custom Attribute[/COLOR][/SIZE] [*][SIZE=5][COLOR=#000000]Xây dựng Custom Attribute[/COLOR][/SIZE] [*][SIZE=5][COLOR=#000000]Áp dụng Attribute trên một phần tử chương trình target[/COLOR][/SIZE] [*][SIZE=5][COLOR=#000000]Truy cập các Attribute thông qua Reflection[/COLOR][/SIZE] [/LIST] [SIZE=5][COLOR=#000000]Bước cuối cùng liên quan tới việc viết một chương trình đơn giản để đọc qua metadata để tìm ra các notation đa dạng. Metadata là dữ liệu hoặc thông tin được sử dụng để miêu tả dữ liệu khác. Chương trình này nên sử dụng các Reflection để truy cập các Attribute tại runtime. Chúng ta sẽ bàn luận điều này trong chương tới.[/COLOR] [COLOR=#0000ff][B]6. Khai báo một Custom Attribute trong C#[/B][/COLOR] [COLOR=#000000]Một Custom Attribute mới nên được kế thừa từ lớp System.Attribute trong C#. Ví dụ:[/COLOR] [I][COLOR=#5900b3]//a custom attribute BugFix to be assigned to a class and its members[/COLOR][/I] [I][COLOR=#5900b3][AttributeUsage(AttributeTargets.Class |[/COLOR][/I] [I][COLOR=#5900b3]AttributeTargets.Constructor |[/COLOR][/I] [I][COLOR=#5900b3]AttributeTargets.Field |[/COLOR][/I] [I][COLOR=#5900b3]AttributeTargets.Method |[/COLOR][/I] [I][COLOR=#5900b3]AttributeTargets.Property,[/COLOR][/I] [I][COLOR=#5900b3]AllowMultiple = true)][/COLOR][/I] [COLOR=#000000]public class DeBugInfo : System.Attribute Trong code trên, chúng ta đã khai báo một Custom Attribute là [I]DeBugInfo[/I].[/COLOR] [COLOR=#0000ff][B]7. Xây dựng Custom Attribute trong C#[/B][/COLOR] [COLOR=#000000]Chúng ta cùng xây dựng Custom Attribute có tên là [I]DeBugInfo[/I], mà lưu giữ thông tin thu được bởi việc debug bất kỳ chương trình nào. Nó có thể giữ thông tin sau:[/COLOR][/SIZE] [LIST] [*][SIZE=5][COLOR=#000000]Số hiệu code để bug[/COLOR][/SIZE] [*][SIZE=5][COLOR=#000000]Tên lập trình viên, người nhận diện bug đó[/COLOR][/SIZE] [*][SIZE=5][COLOR=#000000]Ngày review cuối cùng của code đó[/COLOR][/SIZE] [*][SIZE=5][COLOR=#000000]Một thông báo dạng chuỗi để lưu giữ các lưu ý của lập trình viên[/COLOR][/SIZE] [/LIST] [SIZE=5][COLOR=#000000]Lớp [I]DeBugInfo[/I] có 3 thuộc tính private để lưu giữ 3 thông tin đầu tiên và một 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, và ngày review là các tham số vị trí tương ứng của lớp DeBugInfo và thông báo là một tham số tùy ý.[/COLOR] [COLOR=#000000]Mỗi Attribute phải có ít nhất một constructor. Các tham số vị trí tương ứng nên được truyền thông qua constructor đó. Ví dụ sau minh họa lớp [I]DeBugInfo[/I] trên:[/COLOR] [COLOR=#000000]//Vi du minh hoa mot custom attribute BugFix[/COLOR] [I][COLOR=#5900b3][AttributeUsage(AttributeTargets.Class |[/COLOR][/I] [I][COLOR=#5900b3]AttributeTargets.Constructor |[/COLOR][/I] [I][COLOR=#5900b3]AttributeTargets.Field |[/COLOR][/I] [I][COLOR=#5900b3]AttributeTargets.Method |[/COLOR][/I] [I][COLOR=#5900b3]AttributeTargets.Property,[/COLOR][/I] [I][COLOR=#5900b3]AllowMultiple = true)][/COLOR][/I] [I][COLOR=#5900b3]public class DeBugInfo : System.Attribute[/COLOR][/I] [I][COLOR=#5900b3]{[/COLOR][/I] [I][COLOR=#5900b3] private int bugNo;[/COLOR][/I] [I][COLOR=#5900b3] private string developer;[/COLOR][/I] [I][COLOR=#5900b3] private string lastReview;[/COLOR][/I] [I][COLOR=#5900b3] public string message;[/COLOR][/I] [I][COLOR=#5900b3] [/COLOR][/I] [I][COLOR=#5900b3] public DeBugInfo(int bg, string dev, string d)[/COLOR][/I] [I][COLOR=#5900b3] {[/COLOR][/I] [I][COLOR=#5900b3] this.bugNo = bg;[/COLOR][/I] [I][COLOR=#5900b3] this.developer = dev;[/COLOR][/I] [I][COLOR=#5900b3] this.lastReview = d;[/COLOR][/I] [I][COLOR=#5900b3] }[/COLOR][/I] [I][COLOR=#5900b3] [/COLOR][/I] [I][COLOR=#5900b3] public int BugNo[/COLOR][/I] [I][COLOR=#5900b3] {[/COLOR][/I] [I][COLOR=#5900b3] get[/COLOR][/I] [I][COLOR=#5900b3] {[/COLOR][/I] [I][COLOR=#5900b3] return bugNo;[/COLOR][/I] [I][COLOR=#5900b3] }[/COLOR][/I] [I][COLOR=#5900b3] }[/COLOR][/I] [I][COLOR=#5900b3] [/COLOR][/I] [I][COLOR=#5900b3] public string Developer[/COLOR][/I] [I][COLOR=#5900b3] {[/COLOR][/I] [I][COLOR=#5900b3] get[/COLOR][/I] [I][COLOR=#5900b3] {[/COLOR][/I] [I][COLOR=#5900b3] return developer;[/COLOR][/I] [I][COLOR=#5900b3] }[/COLOR][/I] [I][COLOR=#5900b3] }[/COLOR][/I] [I][COLOR=#5900b3] [/COLOR][/I] [I][COLOR=#5900b3] public string LastReview[/COLOR][/I] [I][COLOR=#5900b3] {[/COLOR][/I] [I][COLOR=#5900b3] get[/COLOR][/I] [I][COLOR=#5900b3] {[/COLOR][/I] [I][COLOR=#5900b3] return lastReview;[/COLOR][/I] [I][COLOR=#5900b3] }[/COLOR][/I] [I][COLOR=#5900b3] }[/COLOR][/I] [I][COLOR=#5900b3] [/COLOR][/I] [I][COLOR=#5900b3] public string Message[/COLOR][/I] [I][COLOR=#5900b3] {[/COLOR][/I] [I][COLOR=#5900b3] get[/COLOR][/I] [I][COLOR=#5900b3] {[/COLOR][/I] [I][COLOR=#5900b3] return message;[/COLOR][/I] [I][COLOR=#5900b3] }[/COLOR][/I] [I][COLOR=#5900b3] set[/COLOR][/I] [I][COLOR=#5900b3] {[/COLOR][/I] [I][COLOR=#5900b3] message = value;[/COLOR][/I] [I][COLOR=#5900b3] }[/COLOR][/I] [I][COLOR=#5900b3] }[/COLOR][/I] [I][COLOR=#5900b3]}[/COLOR][/I] [COLOR=#0000ff][B]8. Áp dụng Custom Attribute trong C#[/B][/COLOR] [COLOR=#000000]Custom Attribute trong C# được áp dụng bằng việc đặt nó ngay trước target của nó:[/COLOR] [I][COLOR=#5900b3][DeBugInfo(45, "Tran Nam", "2/8/2016", Message = "Kieu tra ve khong hop le")][/COLOR][/I] [I][COLOR=#5900b3][DeBugInfo(49, "Minh Chinh", "10/10/2016", Message = "Bien chua duoc su dung")][/COLOR][/I] [I][COLOR=#5900b3]class Rectangle[/COLOR][/I] [I][COLOR=#5900b3]{[/COLOR][/I] [I][COLOR=#5900b3] //cac bien thanh vien[/COLOR][/I] [I][COLOR=#5900b3] protected double chieu_dai;[/COLOR][/I] [I][COLOR=#5900b3] protected double chieu_rong;[/COLOR][/I] [I][COLOR=#5900b3] public Rectangle(double l, double w)[/COLOR][/I] [I][COLOR=#5900b3] {[/COLOR][/I] [I][COLOR=#5900b3] chieu_dai = l;[/COLOR][/I] [I][COLOR=#5900b3] chieu_rong = w;[/COLOR][/I] [I][COLOR=#5900b3] }[/COLOR][/I] [I][COLOR=#5900b3] [DeBugInfo(55, "Tran Nam", "2/8/2016", Message = "Kieu tra ve khong hop le")][/COLOR][/I] [I][COLOR=#5900b3] [/COLOR][/I] [I][COLOR=#5900b3] public double tinhDienTich()[/COLOR][/I] [I][COLOR=#5900b3] {[/COLOR][/I] [I][COLOR=#5900b3] return chieu_dai * chieu_rong;[/COLOR][/I] [I][COLOR=#5900b3] }[/COLOR][/I] [I][COLOR=#5900b3] [DeBugInfo(56, "Minh Chinh", "10/10/2016")][/COLOR][/I] [I][COLOR=#5900b3] [/COLOR][/I] [I][COLOR=#5900b3] public void Display()[/COLOR][/I] [I][COLOR=#5900b3] {[/COLOR][/I] [I][COLOR=#5900b3] Console.WriteLine("Chieu dai: {0}", chieu_dai);[/COLOR][/I] [I][COLOR=#5900b3] Console.WriteLine("Chieu rong: {0}", chieu_rong);[/COLOR][/I] [I][COLOR=#5900b3] Console.WriteLine("Dien tich: {0}", tinhDienTich());[/COLOR][/I] [I][COLOR=#5900b3] }[/COLOR][/I] [I][COLOR=#5900b3]}[/COLOR][/I] [COLOR=#000000]Trên đây là bài giới thiệu về attribute trong C# và cách sử dụng nó. Chúc các bạn một ngày học tập vui vẻ, hiệu quả.[/COLOR] [/SIZE] [RIGHT][SIZE=5][COLOR=rgb(0, 0, 0)]Nguồn: vietjack.com[/COLOR][/SIZE][/RIGHT] [/QUOTE]
Tên
Mã xác nhận
Gửi trả lời
CÔNG NGHỆ
Công Nghệ Thông Tin
Code
Tìm hiểu về Attribute trong C#
Top