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
-
Buy cheap Xanax overnight. Cheap Xanax. Overnight delivery of Xanax in US no prescription needed. Cheapest Xanax.
by asleymar (0 replies)
-
Buy Soma online without a prescription. Soma drug no prescription. How to get Soma prescription. Soma cod accepted.
by asleymar (0 replies)
-
Cheap online order Fioricet. Cheap discount Fioricet. Offshore Fioricet online. How to buy Fioricet online without a prescription.
by asleymar (0 replies)
-
Buy Ambien no visa without prescription. Not expensive Ambien prescriptions. Ambien no rx. Cod delivery Ambien.
by asleymar (0 replies)
-
Tramadol without doctor rx. Buy Tramadol over the counter cod overnight. Cheap Tramadol cod delivery. Buy Tramadol from mexico online.
by asleymar (0 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
15
DevWeek 2010
London, United Kingdom
DevWeek is Europe’s leading independent conference for software developers, database professionals and IT architects, and features expert speakers on a wide range of topics, including .NET 4.0, Silverlight 3, WCF 4, Visual Studio 2010, REST, Windows Workflow 4, Thread Synchronization, ASP.NET 4.0, SQL Server 2008 R2, LINQ, Unit Testing, CLR & C# 4.0, .NET Patterns, WPF 4, F#, Windows Azure, ADO.NET, Entity Framework, Debugging, T-SQL Tips & Tricks, and more.
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#.