C# property attributes(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# 属性属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:C# 属性属性
基础教程推荐
- 将 XML 转换为通用列表 2022-01-01
- c# Math.Sqrt 实现 2022-01-01
- 为什么Flurl.Http DownloadFileAsync/Http客户端GetAsync需要 2022-09-30
- 如何在 IDE 中获取 Xamarin Studio C# 输出? 2022-01-01
- rabbitmq 的 REST API 2022-01-01
- 如何激活MC67中的红灯 2022-01-01
- 将 Office 安装到 Windows 容器 (servercore:ltsc2019) 失败,错误代码为 17002 2022-01-01
- 有没有办法忽略 2GB 文件上传的 maxRequestLength 限制? 2022-01-01
- MS Visual Studio .NET 的替代品 2022-01-01
- SSE 浮点算术是否可重现? 2022-01-01