We need you!

We're working hard on the next version of Developer Fusion. Let us know what you think we should be up to!

Members

Technology Zones

Articles

Hosted By

MaximumASP

Info

Rated
Read 35,719 times

Contents

Related Categories

Attributed Programming in .NET Using C# - Introduction

ddutta

Introduction

An attribute is a powerful .NET language feature that is attached to a target programming element (e.g., a class, method, assembly, interface, etc.) to customize behaviors or extract organizational information of the target at design, compile, or runtime.

The paradigm of attributed programming first appeared in the Interface Definition Language (IDL) of COM interfaces. Microsoft extended the concept to Transaction Server (MTS) and used it heavily in COM+. It is a clean approach to associate metadata with program elements and later use the metadata at design, compile or run time to accomplish some common objectives. In .NET, Microsoft went a step further by allowing the implementation of attributes in the source code, unlike the implementation in MTS and COM+ where attributes were defined in a separate repository. To understand the power of attributes, consider the serialization of an object. In .NET, you just need to mark a class Serializable to make its member variables as Serializable.

For example:

using System;
using System.IO;
using System.Runtime.Serialization.Formatters.Soap;

[Serializable]
public class User{
     public string userID;
     public string password;
     public string email;
     public string city;

     public void Save(string fileName){
          FileStream s=new FileStream(fileName,FileMode.Create);
          SoapFormatter sf=new SoapFormatter();
          sf.Serialize(s,this);
     }
     
     static void Main(string[] args){
          User u=new User();
          u.userID="firstName";
          u.password="Zxfd12Qs";
          u.email="asdf@qwer.com";
          u.city="TheCity";
          u.Save("user.txt");
     }
}

Note: You may have to Add a reference to assembly System.Runtime.Serialization.Formatters.Soap.dll

The above example illustrates the power of attributes. We do not have to tell what to serialize; we just need to mark the class as serializable by annotating the class with Serializable attribute. Of course, we need to tell the serialization format (as in the Save method).

Comments

  • Why attribute

    Posted by soruabhm on 03 May 2004

    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 understa...

  • Serialization SOAP

    Posted by zaka48 on 06 Mar 2004

    how can I Add a reference to assembly System.Runtime.Serialization.Formatters.Soap.dll ???

  • Private attributes in .Net c#?

    Posted by nguyen422 on 10 Jan 2003

    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 usin...

  • Attributes

    Posted by chinnu704 on 06 Jan 2003

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

  • Drawbacks of attributed programming

    Posted by ghanshyam on 12 Nov 2002

    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 ...