.net属性的优势? [英] Advantage of Attributes in .net?

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

问题描述

你好。

.NET框架成员中属性类的好处和用法是什么?

如下:

hello there.
what is benefits and usages of Attribute classes in .NET framework members?
like following:

[Serializable()]
   [XmlRoot(ElementName = "Preferences", Namespace = UserPreferenceUtility.SchemaNamespace, IsNullable = false)]
   public partial class Preferences : INotifyPropertyChanged
   {

       [XmlElement(ElementName = "ReceiptImport")]
       public ImportDialog _ReceiptImport;



以及编译器如何处理Serializable()等

谢谢。


and how compiler acts with Serializable() ,etc
Thanks.

推荐答案

C#属性编程快速概述 [< a href =http://www.codeproject.com/Articles/390422/Quick-Overview-of-Csharp-Attribute-Programmingtarget =_ blanktitle =New Window> ^ ]是一个描述属性如何有用的简洁文章。
Quick Overview of C# Attribute Programming[^] is a neat article that describes how attributes can be useful.


首先,请看我的答案。根据我在本评论中解释的原因,我不打算讨论整个主题。我只会尝试给你一些注释,这些注释可能会帮助你注意这个主题的某些方面。



首先,我会注意到 SerializeableAttribute 不是属性优点的最佳示例。相关的序列化机制在道德上已经过时,但从技术上讲仍然需要它。有更智能的机制,更灵活,更易于使用:数据合同 http://msdn.microsoft.com/en-us/library/ms733127.aspx [ ^ ]。



最大的实际问题是此机制仅适用于.NET FCL用于基于XML和JSON的序列化。它不包括二进制和SOAP。我甚至使用相同的想法编写了另一种机制,并且具有良好性能的序列化实现需要使用 System.Reflection.Emit ,这很难调试和需要深入了解IL。 (使用这种技术是因为序列化很慢,所以序列化代码是预生成的,可以重复使用。)



所以,我对属性的注释将更多地与数据合约,少于 SerializeableAttribute 。重要的是要理解属性只是将一些元数据添加到代码中。换句话说,它们不会影响程序集,类型和成员的通常行为。它们仅静态地提供可以使用反射在代码中使用的一些信息。比如,一个属性可以告诉这段代码(在这种情况下是序列化程序),序列化的内容和不是什么。如果给我们怎么办?这些特征包括:1)非侵入式方法,2)关注点分离松散耦合



换句话说,当你添加一个新属性时,你什么都不做,你正在添加一些不能改变现有功能的东西。您可以正常使用这些类型,就好像属性不存在一样。只有使用它们的代码,嗯...使用它们,例如,序列化器的代码。一种替代方法是使用序列化接口。这也是分离问题的一种方式,但不是那么清楚。如果在基类型列表中添加接口,则会立即收到编译错误(除非接口完全为空),因此它会强制您编写实现,这可能会让您感到很烦恼。对于属性,它不会发生(如果只有属性目标允许在该点使用它,如果属性类型允许,则没有给定类型的多个属性)。同时,界面更快(这就是为什么序列化程序在运行时生成序列化程序集的原因,如上所述)。



参见:

http://en.wikipedia.org/wiki/Separation_of_concerns [ ^ ],

http://en.wikipedia.org/wiki/Loose_coupling [ ^ ]。



-SA
First of all, please see my answer. I am not going to get into the whole topic, by the reasons I explained in this comment. I'll only try to give you some notes which might help you to pay attention for certain aspects of the topic.

First of all, I would note that the SerializeableAttribute is not the best example of the benefits of the attributes. The related serialization mechanism is morally already obsolete, but technically it is still needed. There is much smarter mechanism, which is more flexible and the easiest to use: data contracts: http://msdn.microsoft.com/en-us/library/ms733127.aspx[^].

The big practical problem is that this mechanism is available .NET FCL only for XML and JSON based serialization. It does not cover binary and SOAP. I've even authored an alternative mechanism using the same idea, and implementation of serialization with good performance requires the use of System.Reflection.Emit, which is really hard to debug and requires deep understanding of IL. (Such techniques are used because serialization is slow, so serialization code is pre-generated to be reusable.)

So, my notes of the attributes will be more related to data contracts, and a bit less to SerializeableAttribute. It's important to understand that attributes merely adds some metadata to code. In other words, they don't effect "usual" behavior of assemblies, types and members. They only statically provide some information which can be used in the code using reflection. Say, an attribute can tell this code (serializer, in this case), what to serialize and what not. What if gives us? Such features as 1) non-intrusive approach, 2) separation of concerns, loose coupling.

In other words, when you add a new attribute, you change nothing, you are adding something which cannot change already existing functionality. You use the types normally, as if the attributes did not exist. And only the code using them, well… use them, for example, the code of the serializer. One alternative is using the serialization interface. It is also a way to separate concerns, but not that clearly. If you add an interface in base type list, you will immediately get a compilation error (unless the interface is completely empty), so it will force your to write implementation, which could be a lot of bothering. With attributes, it cannot happen (if only the attribute target is allows using it at the point, there are no multiple attributes of a given type if the attribute types allows that). At the same time, interface is much faster (that's why serializers generates serialization assemblies at runtime, as I explained above).

See also:
http://en.wikipedia.org/wiki/Separation_of_concerns[^],
http://en.wikipedia.org/wiki/Loose_coupling[^].

—SA


这篇关于.net属性的优势?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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