自定义 .NET 属性的实际使用
您在现实世界中使用了自定义 .NET 属性来做什么?
我读过几篇关于它们的文章,但我从未使用过自定义属性。
我觉得当它们有用时我可能会忽略它们。
我说的是您创建的属性,而不是框架中已包含的属性。
What kind of things have you used custom .NET attributes for in the real world?
I've read several articles about them, but I have never used custom attributes.
I feel like I might be overlooking them when they could be useful.
I am talking about attributes that you create, not ones that are already included in the framework.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
我创建了一个脚本引擎,并使用 [Command] 属性标记各种方法。 这意味着这些函数暴露给脚本引擎。
示例:
并按使用情况:
I created a scripting engine, and tagged various methods with the [Command] attribute. This meant that these functions were exposed to the scripting engine.
Example:
And as used:
我使用它们“自定义”属性进行验证(即标记要使用我自己的“信用卡验证”进行验证的字段)和我编写的自定义 LinqToLucene 分析器(即指定在给定字段上使用哪个分析器) 。
例如,验证代码看起来像这样:
当验证上面的对象时,由于“自定义”属性,每个字段都会使用适当的验证器进行验证。
在 LinqToLucene 中,我编写的自定义属性非常好,因为它们允许您在运行时查找(通过反射)特定字段。 例如,如果您有一个客户对象,您可能有兴趣获取已标记为“索引我”的所有属性:自定义属性可以让您轻松完成此操作,因为它以以下方式公开有关该对象的元数据:很容易查询。
I've used them "custom" attributes for validation (ie. marking a field to be validated with my own "credit card validation") and custom LinqToLucene analyzers I've written (ie. specifying which analyzer to use on a given field).
The validation code, for example, would look something like this:
When the object above is validated, each field is validated with the appropriate validator thanks to the "custom" attribute.
In the LinqToLucene stuff I've written custom attributes are nice because they allow you to find (through reflection) specific fields at run time. For example, if you have a customer object, you may be interested in getting all the properties that have been marked as "index me": a custom attribute lets you do this easily since it exposes meta-data about the object in a manner that is easy to query.
除此之外,我还使用它们来指定 EBNF,该 EBNF 在运行时读取以动态创建自定义解析器,并指定有关数据库字段的元数据。
我发现我经常使用自定义属性的一种“模式”是替换枚举,特别是当代码中的差异位置存在对枚举的依赖性时。
例如,我可能有一个对象状态的枚举。 基于这种状态,我在代码中可能有 3 或 4 个不同的位置,我将对该枚举进行“切换”并执行一些操作。 其他一些开发人员可以通过添加新枚举而不是在代码中其他位置的 switch 语句之一中进行处理来轻松引入错误。
因此,为了避免这种情况,我创建了一个声明为静态类的自定义属性。 自定义属性在类的静态构造函数中加载到字典中,并且代码中的所有位置都使用字典而不是 switch 语句。 自定义属性构造函数包含每个 switch 语句的“硬编码”值。
Among other things, I've used them to specify EBNF which is read at run-time to create custom parsers on the fly and also to specify metadata about fields for a database.
I find one 'pattern' I'm commonly using custom attributes is to replace enums especially when there is a dependency on the enum in diff places in code.
E.g. I might have an enum for a state of an object. Based on this state, I have maybe 3 or 4 different places in code which I would do a 'switch' of that enum and perform some operation. Some other developer could easily introduce a bug by adding a new enum but not handling in one of the switch statements somewhere else in code.
So to avoid this I create a custom attributes declared to a static class. The custom attributes are loaded in the static constructor of the class into a dictionary and all places in code use the dictionary instead of switch statements. The custom attribute constructor contains the 'hard-coded' values for each switch statement.
我必须将一些对象序列化为自定义(旧)格式,并且我使用属性来标识应序列化哪些字段以及如何格式化它们。 然后我有一个序列化器,它可以获取具有这些属性的任何对象并使用反射来格式化它。
I had to serialize some objects to a custom (legacy) format, and I used attributes to identify which fields should be serialized and how to format them. Then I had a serializer that could take any object with these attributes and use reflection to format it.
到目前为止,我还没有真正找到自定义属性的用途。 在某些情况下,我认为它们可能是合适的,但没有使用它们,因为显然读取自定义属性所涉及的反射非常昂贵。
I haven't really found a use for custom attributes as of yet. There have been a few situations where I thaught they may be appropriate but didn't use them because apparently the reflection involved in reading custom attributes is quite expensive.
我已经在“插件”DLL 中的类上放置了自定义属性。 这允许框架动态发现可用的插件,评估它们是否感兴趣,然后动态加载感兴趣的插件。
在我们的领域中,示例是对家庭中的特定车辆进行建模的插件。 用于车辆系列的一个插件实际上可能对该车辆系列内的多个车辆模型进行建模(例如,“MX-6”、“Probe”)。 如果 ID 或模型名称作为自定义属性数组包含在内,我们可以快速忽略任何甚至没有自定义属性的 DLL,然后进一步忽略任何不为感兴趣的车辆建模的 DLL。
I have placed custom attributes on classes within "plug-in" DLLs. This allows a framework to dynamically discover available plug-ins, evaluate whether they are of interest, and then dynamically load the ones of interest.
In our domain, the example is plug-ins which model particular vehicles within a family. One plug-in for a vehicle family might actually model several vehicle models within the vehicle family (e.g., "MX-6", "Probe"). If an ID or Model Name is included as a custom attribute array, we can quickly ignore any DLLs that don't even have custom attributes, and then further ignore any that do not model the vehicle of interest.
我在我基于 ActiveRecord 模式开发的 ORM 框架之一中使用了它。 这与 LINQ、Castle 项目等中可用的实现类型相同。
该框架称为“SkyFramework”,但它不是开源的。
例如,只是一个粗略的示例...
您也会在其他开源项目中找到类似的示例。
注意:“Table”、“Columns”属性是当时的自定义属性。
ActiveRecord 引擎解析这些属性的对象并为 CRUD 生成相应的函数...等等...
类似地,我开发了一些自定义属性来识别需要进行基准测试的代码部分...例如。
标记有上述属性的方法会自动进行基准测试并生成日志。 这些是一些早期的实现。
Apress 有一本关于这个主题的书。
应用 .NET 属性可能对您有帮助。
I had used it in one of the ORM frameworks which I developed based on the ActiveRecord pattern. This is the same kind of implementation that is available in LINQ, Castle project etc.
The framework was called "SkyFramework", but it was not opensource.
for e.g. Just a rough example...
You will find similar examples in other opensource projects as well.
NOTE: The attribute "Table", "Columns" were the custom attributes at that time.
The ActiveRecord engine parses the object for these attributes and generates the respective functions for CRUD... etc...
Similarly, I had developed some custom attributes for identifying portions of code that needs to be benchmarked...for e.g..
The methods marked with the above attributes are automatically bench marked and a log is generated. These were some earlier implementations.
There's an Apress book available on the topic..
Applied .NET Attribues which may be of help to you.