微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何在 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# 中使用属性的详细内容,更多请关注编程之家其它相关文章

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐