Understanding .net Attribute For Batter Programming-holle树先生�����

Web-Development The .NET platform provides a way for programmers to embed additional metadata into an assembly using attributes. In a nutshell, attributes ate nothing more than code annotations that can be applied to a given type, member, assembly, or module. The idea of annotating code using attributes is not new. COM IDL provided numerous predefined attributes that allowed developers to describe the types contained within a given COM server. How-ever, COM attributes were little more then a set of keywords. If a COM developer needed to create a custom attribute, they could do so, but it was referenced in code by a 128-bit number (GUID), which was cumbersome at best. Unlike COM ICL attributes (which again were simply keywords), .NET attributes are class types that extend the abstract system. Attributes base class. As you explore the .NET namespace, you will find many predefined attributes that you are able to make use of in your applications. Furthermore, you are free to build custom attributes to further quality the behavior of your types by creating a new type deriving from attributes. Understand that when you apply attributes in your code, the embedded metadata is essentially useless until another piece of software explicitly reflect over the information. If this is not eh case, the blurb of metadata embedded within the assembly ignored and completely harmless. The .NET framework 2.0 SDK ships with numerous utilities that are indeed on the lookout for various attributes. The C# compiler (csc.exe) itself has been preprogrammed to discover the presence of various attributes during the compilation cycle. If the C# compiler encounters the attribute, it will automatically check the attributed item to ensure it is exposing only CLS-compliant construct, By way of another example, if the C# compiler discovers an item attributed with the attribute, it will display a compiler warning in the visual studio error list window. In addition to development tools, numerous methods in the .NET base class libraries are preprogrammed to reflect over specific attributes. For example, if you wish to persist the state of an object to file you are required to do is annotate your class with the attribute. If the serialize () method of the binary Formatter class encounters this attribute, the object is automatically persisted to file in a compact binary format. The .NET CLR is also on the prowl for the presence of certain attributes. Perhaps the most famous. NET attribute is web method. If you wish to expose a method via HTTP requests and automatically encode the method return value as XML, simply apply web method to the method and the CLR handles the details. Beyond web service development, attributes are critical to the operation of the .NET security system, .NET removing layer, and COM/.NET interoperability. You are free to build applications that are programmed to reflect over your own custom attributes as well as any attribute in the .NET base class libraries. By doing so, you are essentially able to create a set of keywords that are understood by a specific set of assemblies. About the Author: 相关的主题文章: