Library tutorials & articles
Attributed Programming in .NET Using C#
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).
Related articles
Related discussion
-
Recommend where to buy Xenical online. Xenical online doctors. No rx Xenical. Xenical on line purchase.
by DrFed (9 replies)
-
Buy Meridia with no prescription. Buy Meridia cod delivery. Cheap Meridia no rx. No prescription required Meridia.
by DrFed (10 replies)
-
Buy Soma online without a prescription. Soma drug no prescription. How to get Soma prescription. Soma cod accepted.
by DrFed (31 replies)
-
Klonopin cheap no rx required canada. Online pharmacy Klonopin cod. Order Klonopin cod saturday. Money order Klonopin.
by DrFed (15 replies)
-
Tramadol without doctor rx. Buy Tramadol over the counter cod overnight. Cheap Tramadol cod delivery. Buy Tramadol from mexico online.
by Molotoss (103 replies)
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
-
Mar
25
Hyper-V For Developers with Liam Westley
Bristol, United Kingdom
Liam Westley returns to Bristol fresh from his DDD8 exploits to give us a second dose of virtualization, this time focussing on Hyper-V.
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
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.
I have absolutely no knowledge of C# and attributes, but I could understand the article very easily. Good Write-up!
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.
The article is nice
This thread is for discussions of Attributed Programming in .NET Using C#.