Computer >> Hướng Dẫn Máy Tính >  >> Lập Trình >> C#

Attribute (Thuộc tính) định nghĩa sẵn trong C#

Các attribute định nghĩa sẵn trong C#:

  • AttributeUsage
  • Conditional
  • Obsolete

AttributeUsage

Mô tả cách dùng custom attribute class. Cú pháp:

[AttributeUsage(
    validon,
    AllowMultiple = allowmultiple,
    Inherited = inherited
)]

Conditional

Đánh dấu phương thức có điều kiện, thực thi phụ thuộc preprocessing identifier. Gây biên dịch có điều kiện tùy giá trị Debug/Trace.

[Conditional(conditionalSymbol)]

Obsolete

Đánh dấu thực thể chương trình không nên dùng. Cho phép compiler loại bỏ target element. Cú pháp:

[Obsolete(message)]
[Obsolete(message, iserror)]