现实世界中使用自定义.NET属性 [英] Real world use of custom .NET attributes

查看:149
本文介绍了现实世界中使用自定义.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.

推荐答案

我用他们的自定义属性进行验证(即标记字段与我自己的信用卡验证进行验证)和自定义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).

验证code,例如,将是这个样子:

The validation code, for example, would look something like this:

public class Customer
{
     [CreditCardValidator]
     string creditCardNumber;

     [AddressValidator]
     string addressLineOne
}

在上面的对象进行验证,每个域的验证与相应的验证多亏了自定义属性。

When the object above is validated, each field is validated with the appropriate validator thanks to the "custom" attribute.

在LinqToLucene的东西,我已经写了自定义属性都不错,因为他们让你找到(通过反射)在运行时的具体领域。例如,如果你有一个客户对象,你可能有兴趣在得到所有被标记为属性指标我:一个自定义的属性可以让你做到这一点很容易,因为它暴露了有关的方式对象的元数据很容易地查询。

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.

这篇关于现实世界中使用自定义.NET属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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