C# 属性属性 [英] C# property attributes

查看:21
本文介绍了C# 属性属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我看过以下代码:

[DefaultValue(100)]
[Description("Some descriptive field here")]
public int MyProperty{...} 

上述代码段的功能似乎很清楚,我不知道如何使用它来做有用的事情.我什至不确定要给它起什么名字!

The functionality from the above snippit seems clear enough, I have no idea as to how I can use it to do useful things. Im not even sure as to what name to give it!

有谁知道我在哪里可以找到有关这些属性属性的更多信息/教程?我也会对此功能可以执行的任何新颖/有用的任务感兴趣.

Does anyone know where I can find more information/a tutorial on these property attributes? I would be also interested in any novel / useful tasks this feature can do.

推荐答案

人们已经涵盖了 UI 方面 - 属性还有其他用途,不过……例如,它们在大多数序列化框架中被广泛使用.编译器对某些属性进行了特殊处理 - 例如,[PrincipalPermission(...)] 为方法添加声明性安全性,允许您(自动)检查用户是否具有合适的访问权限.

People have already covered the UI aspect - attributes have other uses, though... for example, they are used extensively in most serialization frameworks. Some attributes are given special treatment by the compiler - for example, [PrincipalPermission(...)] adds declarative security to a method, allowing you to (automatically) check that the user has suitable access.

要添加自己的特殊处理,可以使用 PostSharp;有很多使用 PostSharp 做 AOP 事情的好例子,比如日志记录 - 或者只是代码简化,比如 自动 INotifyPropertyChanged 实现.

To add your own special handling, you can use PostSharp; there are many great examples of using PostSharp to do AOP things, like logging - or just code simplification, such as with automatic INotifyPropertyChanged implementation.

这篇关于C# 属性属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆