Closed Thread
Results 1 to 4 of 4

Thread: .NET attributes are more than decoration

  1. #1
    Join Date
    Aug 2008
    Posts
    5
    Rep Power
    0

    .NET attributes are more than decoration

    Among the most confusing and misunderstood elements of the .NET framework are the purpose and uses of attributes. Read this article to see why attributes are a good thing.

    Since attributes are new to both C++ and VB developers, there’s no context for easy comparisons to familiar language elements. But the addition of attributes to the Common Language Runtime (CLR) gives developers new abilities to associate information with their classes via an annotation mechanism, which the CLR can then use to operate on the objects at runtime.

    Attributes can be used to document classes at design time, specify runtime information (such as the name of an XML field to be used when serialising information from the class), and even dictate runtime behavior (such as whether the class should automatically participate in a transaction). More importantly, you can develop your own attributes that specify architectural standards and enforce their use. In this article, we’ll look at how the CLR uses standard attributes and how and why you should create your own attributes.

    What is an attribute?
    Many .NET developers get their first exposure to attributes when using templates provided in the Visual Studio environment. For example, when a VB developer creates a new XML Web Service, they get back sample code that defines the Web Service to the CLR using attributes like this:

    _
    Public Class Service1
    Inherits System.Web.Services.WebService

    The class Service1 is said to have been “decorated” with the WebService attribute, and the NameSpace variable has been assigned the value of tempuri.org. The WebService and WebMethod attributes signal the compiler that these attributes should be accessible using the SOAP protocol. As you can see from this example, the purpose of .NET attributes is to signal a compiler or the runtime to generate MSIL or to operate on the MSIL generated, based on metadata representing the attribute. There are many other examples of using attributes to instruct the compiler how to generate the appropriate MSIL, including:

    * Using MarshalAsAttribute to tell the compiler how to marshal method parameters when interoperating with native code.
    * Using COMClassAttribute to mark components as COM so the Visual Basic compiler will generate code allowing a .NET component to be called from COM.
    * Using attributes to describe the resulting assembly with title, version, or description information. The version information is especially important when using signed assemblies and the Global Assembly Cache because you can force the runtime to load only particular versions of assemblies and avoid the COM DLL Hell problem.
    * Mapping class members to specific XML node types when defining XML serialisation.

    When compiled, attributes are saved with the metadata of the finished assembly. At runtime, the CLR or your own programs can still use any attributes used by the compiler to control code generation by using reflection to query the assembly for the values specified by an attribute. The feature that makes attributes most powerful, however, is their ability to add additional capabilities to any language hosted within the .NET runtime without making changes to the language compilers.

  2. CODECALL Circuit advertisement
    Join Date
    Always
    Posts
    Many

     
  3. #2
    Join Date
    Mar 2008
    Location
    The North Pole
    Posts
    13,174
    Blog Entries
    13
    Rep Power
    114

    Re: .NET attributes are more than decoration

    Tutorıal?

    Quote Originally Posted by Jordan View Post
    Good members, like yourself, stick around and post for ages to come!
    Mr. Xav | Blog | Forums

  4. #3
    Jordan Guest

    Re: .NET attributes are more than decoration

    More like a blog. Good article though.

  5. #4
    gaylo565's Avatar
    gaylo565 is offline Programming Professional
    Join Date
    May 2007
    Location
    flagstaff, az
    Posts
    268
    Rep Power
    21

    Re: .NET attributes are more than decoration

    Both of your articles are very informative They might be more of a blog than a forum post but +rep given anywho.

Closed Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. File Attributes
    By Hunter100 in forum C and C++
    Replies: 5
    Last Post: 08-13-2010, 04:11 PM
  2. LINQ: How to Read XML PARAMETER Attributes
    By SeekAndFind in forum C# Programming
    Replies: 2
    Last Post: 05-06-2010, 09:12 AM
  3. CSS Part 4: Common Attributes
    By Lop in forum Tutorials
    Replies: 4
    Last Post: 02-08-2009, 04:39 AM
  4. CSS Part 2: Common Attributes
    By Lop in forum Tutorials
    Replies: 6
    Last Post: 11-30-2008, 01:35 PM
  5. Links Decoration
    By Jaan in forum Tutorials
    Replies: 1
    Last Post: 01-07-2007, 08:45 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts