Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / New Users / May 2004

Tip: Looking for answers? Try searching our database.

Why use attributes?

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
sourabh - 03 May 2004 09:51 GMT
Hi
I have a very basic question here - why do we need attributes or basically
how we decide when to use attributes?
I have used a lot of .net inbuilt attributes, but have not been really able
to put this great feature to use by defining custom attributes. I know how
to use them but the bigger question is when should we use them.

Perhaps this might be a very simple question to the .net world. All help is
welcome.

Thanks
Sourabh
mikeb - 03 May 2004 16:44 GMT
> Hi
> I have a very basic question here - why do we need attributes or basically
> how we decide when to use attributes?
> I have used a lot of .net inbuilt attributes, but have not been really able
> to put this great feature to use by defining custom attributes. I know how
> to use them but the bigger question is when should we use them.

Attributes are a way to package information *about* your code with your
code.  Programs can read this information using reflection and use that
information to interact with your code in interesting ways.

A good example of when you might create and use a custom attribute is
Jon Skeet's micro-benchmark framework:

    http://www.yoda.arachsys.com/csharp/benchmark.html

In that framework, he defines a custom attribute, 'BenchmarkAttribute'.

In the code that you want benchmarked, you simply add the [Benchmark]
attribute marker to each method that you want the benchmark framework to
time.  When the benchmark framework runs, it looks for all the methods
with a particular signature that are marked with the BenchmarkAttribute,
and it times the execution of those methods.

Signature

mikeb

Jay B. Harlow [MVP - Outlook] - 03 May 2004 16:54 GMT
Sourabh,
Martin Fowler wrote an article explaining why use attributes.

See:
http://martinfowler.com/ieeeSoftware/netAttributes.pdf

From:
http://martinfowler.com/articles.html

Hope this helps
Jay

> Hi
> I have a very basic question here - why do we need attributes or basically
[quoted text clipped - 8 lines]
> Thanks
> Sourabh
Cowboy \(Gregory A. Beamer\) - 03 May 2004 18:03 GMT
Attributes add some information about the code, as Mike has suggested, but
it gets more involved than this.

Try this:
Go to SourceForge.com and download nUnit. Install the code and look at how
nUnit can help you with unit tests. Basically any bit of code set up with
the proper attribute will be run, via reflection, in your compiled assembly.
This, to me, best illustrates the use of attributes.

Signature

Gregory A. Beamer
MVP; MCP: +I, SE, SD, DBA

**********************************************************************
Think Outside the Box!
**********************************************************************

> Hi
> I have a very basic question here - why do we need attributes or basically
[quoted text clipped - 8 lines]
> Thanks
> Sourabh
som som - 04 May 2004 05:40 GMT
Thanks. For all your replies. These would really make my understanding
better.

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.