Are there specific cases when one should use custom attributes on class instead of properties? I know that properties are preferrable because of their discoverability and performance, but attributes... When should I definitely use them?
是否有特定情况应该在类而不是属性上使用自定义属性?我知道属性是可取的,因为它们的可发现性和性能,但属性......我应该什么时候使用它们?
UPDATE:
Here is a post by Eric Lippert about this decision.
以下是Eric Lippert关于此决定的帖子。
2 个解决方案
#1
Eric Lippert has a great blog post tackling exactly this decision.
Eric Lippert有一篇很棒的博客文章正好解决了这个问题。
His summary is:
他的总结是:
In short: use attributes to describe your mechanisms, use properties to model the domain.
简而言之:使用属性来描述您的机制,使用属性来建模域。
I'd also add to that the consideration that an attribute value is effectively static - in other words it's part of the description of the type rather than any instance of the type.
我还要考虑到属性值实际上是静态的 - 换句话说,它是类型描述的一部分,而不是类型的任何实例。
One tricky bit can come when every instance of some base type has to have a property (e.g. a description) but different concrete derived types want to specify descriptions on a per-type basis rather than per-instance. You often end up with virtual properties which always return constants - this isn't terribly satisfactory. I suspect Delphi's class references might help here... not sure.
当某个基类型的每个实例都必须具有属性(例如描述)但是不同的具体派生类型想要基于每个类型而不是每个实例指定描述时,可能会出现一个棘手的位。你经常会得到总是返回常量的虚拟属性 - 这并不是非常令人满意。我怀疑Delphi的类引用可能对此有所帮助......不确定。
EDIT: To give an example of a mechanism, if you decorate a type to say which table it's from in the database, that's describing the data transfer mechanism rather than saying anything about the model of data that's being transferred.
编辑:举一个机制的例子,如果你装饰一个类型来说明它来自数据库中的哪个表,那就是描述数据传输机制,而不是说任何有关正在传输的数据模型。
#2
There are two use cases:
有两种用例:
1) Using a custom attribute that someone else has defined, such as the System.LoaderOptimization attribute that may be used on the Main method. These kinds of attributes are used to direct platform code such as the CLR, WPF, WCF or the debugger to run the code in a certain way, and can be very useful at times. Reading books on various platform topic is a good way to learn when and how to use these attributes.
1)使用其他人定义的自定义属性,例如可以在Main方法上使用的System.LoaderOptimization属性。这些类型的属性用于指导平台代码(如CLR,WPF,WCF或调试器)以某种方式运行代码,并且有时非常有用。阅读各种平台主题的书籍是学习何时以及如何使用这些属性的好方法。
2) Creating your own custom attribute and using it to decorate a class (or method, property, etc). These have no effect unless you also have code that uses Reflection to notice those attribute usages and change the behavior in some way. This usages should be avoided whenever possible because of very poor performance, orders of magnitude larger than, say, accessing a static member of a class.
2)创建自己的自定义属性并使用它来装饰类(或方法,属性等)。除非您还拥有使用Reflection注意那些属性用法并以某种方式更改行为的代码,否则它们无效。应尽可能避免这种用法,因为性能非常差,比访问类的静态成员大几个数量级。
#1
Eric Lippert has a great blog post tackling exactly this decision.
Eric Lippert有一篇很棒的博客文章正好解决了这个问题。
His summary is:
他的总结是:
In short: use attributes to describe your mechanisms, use properties to model the domain.
简而言之:使用属性来描述您的机制,使用属性来建模域。
I'd also add to that the consideration that an attribute value is effectively static - in other words it's part of the description of the type rather than any instance of the type.
我还要考虑到属性值实际上是静态的 - 换句话说,它是类型描述的一部分,而不是类型的任何实例。
One tricky bit can come when every instance of some base type has to have a property (e.g. a description) but different concrete derived types want to specify descriptions on a per-type basis rather than per-instance. You often end up with virtual properties which always return constants - this isn't terribly satisfactory. I suspect Delphi's class references might help here... not sure.
当某个基类型的每个实例都必须具有属性(例如描述)但是不同的具体派生类型想要基于每个类型而不是每个实例指定描述时,可能会出现一个棘手的位。你经常会得到总是返回常量的虚拟属性 - 这并不是非常令人满意。我怀疑Delphi的类引用可能对此有所帮助......不确定。
EDIT: To give an example of a mechanism, if you decorate a type to say which table it's from in the database, that's describing the data transfer mechanism rather than saying anything about the model of data that's being transferred.
编辑:举一个机制的例子,如果你装饰一个类型来说明它来自数据库中的哪个表,那就是描述数据传输机制,而不是说任何有关正在传输的数据模型。
#2
There are two use cases:
有两种用例:
1) Using a custom attribute that someone else has defined, such as the System.LoaderOptimization attribute that may be used on the Main method. These kinds of attributes are used to direct platform code such as the CLR, WPF, WCF or the debugger to run the code in a certain way, and can be very useful at times. Reading books on various platform topic is a good way to learn when and how to use these attributes.
1)使用其他人定义的自定义属性,例如可以在Main方法上使用的System.LoaderOptimization属性。这些类型的属性用于指导平台代码(如CLR,WPF,WCF或调试器)以某种方式运行代码,并且有时非常有用。阅读各种平台主题的书籍是学习何时以及如何使用这些属性的好方法。
2) Creating your own custom attribute and using it to decorate a class (or method, property, etc). These have no effect unless you also have code that uses Reflection to notice those attribute usages and change the behavior in some way. This usages should be avoided whenever possible because of very poor performance, orders of magnitude larger than, say, accessing a static member of a class.
2)创建自己的自定义属性并使用它来装饰类(或方法,属性等)。除非您还拥有使用Reflection注意那些属性用法并以某种方式更改行为的代码,否则它们无效。应尽可能避免这种用法,因为性能非常差,比访问类的静态成员大几个数量级。