Library tutorials & articles

Attributed Programming in .NET Using C#

Attribute Targets and Specifications

All .NET programming elements (assemblies, classes, interfaces, delegates, events, methods, members, enum, struct, and so forth) can be targets of attributes. When they are specified at global scope for an assembly or module, they should be placed immediately after all using statements and before any code. Attributes are placed in square brackets by immediately placing them before their targets, as in

[WebMethod]
public string CapitalCity(string country){
     //code to return capital city of a country
}

In the absence of any target-specifier, the target of the above attribute defaults to the method it is applied to (CapitalCity). However, for global scoped attributes, the target-specifier must be explicitly specifed, as in

[assembly:CLSCompliant(true)]

Multiple attributes can be applied to a target by stacking one on top of another, or by placing them inside a square bracket and then separating adjacent attributes by commas.

Attributes are classes (we will discuss that later) and as such are able to accept parameters in their specifications (like class constructor). There are two types of parameters, positional and named, that attributes accept in their usage. Positional parameters are like constructor arguments and their signature should match one of the constructors of the attribute class. For example,

[assembly:CLSCompliant(true)]

In the above example, CLSCompliant attribute accepts a boolean parameter in one of its constructors and it should be used with a boolean parameter. Positional parameters are always set through constructors of the attribute.

Named parameters are defined as non-static property in the attribute class declaration. They are optional and, when used, their names should exactly match the name of the property defined in the attribute class declaration. For example,

[WebMethod(EnableSession=true)]

In the above attribute usage, EnableSession is a named parameter and it is optional. It also tells us that WebMethod attribute has a property called EnableSession.

Comments

  1. 03 May 2004 at 03:26

    Hi
    Good article.I have a qus here. I have used a ot of attributes and have tried writing mine as well. Now the basic qus of when to write one of our own is still a little difficult for me. I understand simple using [SERIALIZABLE] makes life easier, but to really appreciate this perhaps I would want to understand whats the tought way of doing the same.
    Could anybody explain with an example how attribute is giving ab advantage.


    Thanks
    sourabh

  2. 06 Mar 2004 at 17:38
    how can I Add a reference to assembly System.Runtime.Serialization.Formatters.Soap.dll ???
  3. 10 Jan 2003 at 05:52
    This is a very good article.  

    The example shown used public modifier for the class attributes, is there any way to get it to work with private/protected attributes?  I tried the example code using private and was unable to get it to work.
  4. 06 Jan 2003 at 15:36

    I have absolutely no knowledge of C# and attributes, but I could understand the article very easily.  Good Write-up!

  5. 12 Nov 2002 at 17:52
    Well the attributed programming started from MIDL/COM programming and .NET also supports this concept widely.
    The advantages provided with attributed programming is amazing and simultaneously it has a drawback.
    The growth of attributes in each model like MIDL,MTS/COM+ and .NET is quite alarming.It increases the learning curve for a developer to use that Runtime.
    And it seems whatever the runtime Environment is not able to do is kept as attributes and Developer has to provide it.

    For example
    void  GetData(CMyObject  obj1,CMyObject  &obj2);

    In the above method all C++ compiler recognizes that obj1 is passed by value and obj2 is passed by reference.If the Runtime environment can have the equivalent inteligence then no requirement of marking [SERIALIAZABLE] attribute to the class if we want to pass the object by value.
    And what ever feature is not provided by any of the existing Object Oriented Language can be used as attributes.

    Ghanshyam.
  6. 31 Oct 2002 at 07:28

    The article is nice

  7. 01 Jan 1999 at 00:00

    This thread is for discussions of Attributed Programming in .NET Using C#.

Leave a comment

Sign in or Join us (it's free).

Deepak Dutta

Related podcasts

  • A Practical Look at Silverlight 2 Part 1

    Now that Silverlight 2 is at the Olympics and making a big splash, we wanted to explore this fascinating technology more. Microsoft Silverlight 2 is a cross-browser, cross-platform, and cross-device plug-in for delivering the next generation of .NET based media experiences and rich interactive ap...

Events coming up

  • Dec 9

    GL.net Group Meeting - December 2009

    Gloucester, United Kingdom

    The beginning of this year holiday season will belong to mocks. Ronnie and Stephen will take us for a tour around exciting world of unit testing.

We'd love to hear what you think! Submit ideas or give us feedback