卓越飞翔博客卓越飞翔博客

卓越飞翔 - 您值得收藏的技术分享站
技术文章17183本站已运行3325

如何在 C# 中使用属性

如何在 C# 中使用属性

属性是一种声明性标记,用于向运行时传达有关程序中各种元素(如类、方法、结构、枚举器、程序集等)行为的信息。您可以通过使用属性向程序添加声明性信息。声明性标记由放置在其所用于的元素上方的方括号([ ])表示。

以下是属性的语法:

'
[attribute(positional_parameters, name_parameter = value, ...)]
Element

.Net Framework 提供两种类型的属性:预定义属性和自定义构建属性。

让我们看看如何声明自定义属性 -

'
//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
卓越飞翔博客
上一篇: 在C语言中的格式说明符
下一篇: 返回列表
留言与评论(共有 0 条评论)
   
验证码:
隐藏边栏