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

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

问题描述

我看到下面的code:

I have seen the following code:

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

从上面的这段功能似乎很清楚的是,我不知道,我怎么可以用它做有益的事情。 IM甚至不知道以什么样的名字给它!

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的事情,比如记录了许多很好的例子 - 或者只是code简化,如用的自动 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天全站免登陆