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
-
String was not recognized as a valid DateTime.
by buvanasubi (22 replies)
-
ASP.NET Patterns every developer should know
by AndyGrant2005 (2 replies)
-
help me to get simple requirement
by Slicksim (1 replies)
-
Compatibility Issue on Firefox to display on Cursor Location
by ansari.wajid (0 replies)
-
Cursor Location in Freetextbox
by ansari.wajid (1 replies)
Related podcasts
-
Looking into the C# Crystal Ball with Charlie Calvert and Bill Wagner
One of the most exciting announcements from PDC was the news about C# 4.0 and Visual Studio 2010. With all the excitement and discussion throughout the event about these new developer tools, we reached out to two experts in the fields. Charlie Calvert and Bill Wagner sat down with Keith and Woody...
Events coming up
-
Dec
6
Developing AJAX Web Applications with Castle Monorail
London, United Kingdom
Monorail is the model-view-controller engine of the Castle Project, bringing many of the best ideas of Ruby on Rails to the .NET world. In this talk, David De Florinier and Gojko Adzic show how Monorail makes it easy to develop .NET based AJAX applications, and how to use the Castle Project to build Web 2.0 applications effectively. Come to this session if you are a .NET web developer. Everyone is welcome!
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
how can I Add a reference to assembly System.Runtime.Serialization.Formatters.Soap.dll ???
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.
I have absolutely no knowledge of C# and attributes, but I could understand the article very easily. Good Write-up!
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.
The article is nice
This thread is for discussions of Attributed Programming in .NET Using C#.