+ Reply to Thread
Results 1 to 4 of 4

Thread: .NET attributes are more than decoration

  1. #1
    Newbie hurricanesoftwares is on a distinguished road
    Join Date
    Aug 2008
    Posts
    5

    .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. #2
    Xav
    Xav is offline
    Code Slinger Xav has much to be proud of Xav has much to be proud of Xav has much to be proud of Xav has much to be proud of Xav has much to be proud of Xav has much to be proud of Xav has much to be proud of Xav has much to be proud of Xav has much to be proud of Xav's Avatar
    Join Date
    Mar 2008
    Location
    The North Pole
    Posts
    13,210
    Blog Entries
    13

    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

  3. #3
    Administrator Jordan is a name known to all Jordan is a name known to all Jordan is a name known to all Jordan is a name known to all Jordan is a name known to all Jordan is a name known to all Jordan's Avatar
    Join Date
    Nov 2005
    Location
    Hendersonville, NC
    Posts
    24,556
    Blog Entries
    97

    Re: .NET attributes are more than decoration

    More like a blog. Good article though.

  4. #4
    Programming Professional gaylo565 is a jewel in the rough gaylo565 is a jewel in the rough gaylo565 is a jewel in the rough gaylo565's Avatar
    Join Date
    May 2007
    Location
    flagstaff, az
    Posts
    255

    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.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

     

Similar Threads

  1. Microsoft .NET 3.0
    By Jordan in forum General Programming
    Replies: 7
    Last Post: 12-08-2006, 09:18 AM
  2. .NET without .NET installed
    By NeedHelp in forum Managed C++
    Replies: 6
    Last Post: 10-28-2006, 05:10 PM
  3. Visual Studio .NET Tips and Tricks free ebook
    By Jordan in forum Software Development Tools
    Replies: 2
    Last Post: 09-14-2006, 02:12 PM
  4. What is .NET anyway?
    By DevilsCharm in forum C# Programming
    Replies: 4
    Last Post: 07-31-2006, 07:38 PM
  5. .net
    By Crane in forum Software Development Tools
    Replies: 4
    Last Post: 07-14-2006, 09:50 AM

Bookmarks

Bookmarks

     
        Algorithms and Data Structures

        Java tutorials

        Algorithms Forum

Posting Permissions

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