PC Review


Reply
Thread Tools Rate Thread

Attributes question!

 
 
CSharper
Guest
Posts: n/a
 
      10th Apr 2009
Hi All,

I have seen lot of programs uses attributes.

[Attribute]
public class method()
{
}

I know attribute is nothing but a class derived from Attribute. I
still couldn't wrap my head around when to use and what it does during
run time. Can some one kindly point me to a detailed place where I can
read? Most of the examples are very simple and I don't see any use or
need of those. If you have time, could you kindly explain me what is
this?

Thanks,
 
Reply With Quote
 
 
 
 
Larry Smith
Guest
Posts: n/a
 
      10th Apr 2009
> Hi All,
>
> I have seen lot of programs uses attributes.
>
> [Attribute]
> public class method()
> {
> }
>
> I know attribute is nothing but a class derived from Attribute. I
> still couldn't wrap my head around when to use and what it does during
> run time. Can some one kindly point me to a detailed place where I can
> read? Most of the examples are very simple and I don't see any use or
> need of those. If you have time, could you kindly explain me what is
> this?


An "attribute" simply represents any piece of information you may want to
store about a given entity (class, method, etc.) that can later be read by
3rd-party tools or the code itself at runtime. Why would you want to do
this? For the same reason you would store information about anything.
Someone might need it. Look at "System.ObsoleteAttribute" as one simple
example. By applying this to any method, you're telling the rest of the
world that the method is now considered obsolete. If you now call this
method in your program for instance, a compiler can detect that the method
is obsolete and display a warning for you. Or perhaps someone wants to write
a utility that will display all obsolete methods being called from your
program simply by reading the assembly itself (since the attribute info is
stored there). And what if you want to add your own information about some
entitry, say, which developer wrote a particular method for instance. You
can create your own "MethodAuthorAttribute" that takes the developers's
employee # (or some other appropriate ID) and this can appplied to every
method in your program. The attrbute can then be used for any number of
reasons. Perhaps you want to dump this to a log at runtime if an unexpected
exception is thrown (for diagnostic purposes). The code can determine which
method was involved and then dump the author's ID out by reading that
method's "MethodAuthorAttribute". Your help staff would then be able to
quickly determine which developer originally wrote the function (or maybe
your company's QA staff uses it during internal testing only). This example
is contrived of course but you get the idea. Have a look at the "Inheritance
Hierarchy" section under the "Attribute" class in MSDN
(http://msdn.microsoft.com/en-us/libr...attribute.aspx). These are
the native .NET attributes and many are used for obscure purposes but they
still fulfill their role, namely, they provide information about the
entities they apply to (to those who need it).


 
Reply With Quote
 
CSharper
Guest
Posts: n/a
 
      10th Apr 2009
On Apr 10, 10:58*am, "Larry Smith" <_nospam@_no_spam.com> wrote:
> > Hi All,

>
> > I have seen lot of programs uses attributes.

>
> > [Attribute]
> > public class method()
> > {
> > }

>
> > I know attribute is nothing but a class derived from Attribute. I
> > still couldn't wrap my head around when to use and what it does during
> > run time. Can some one kindly point me to a detailed place where I can
> > read? Most of the examples are very simple and I don't see any use or
> > need of those. If you have time, could you kindly explain me what is
> > this?

>
> An "attribute" simply represents any piece of information you may want to
> store about a given entity (class, method, etc.) that can later be read by
> 3rd-party tools or the code itself at runtime. Why would you want to do
> this? For the same reason you would store information about anything.
> Someone might need it. Look at "System.ObsoleteAttribute" as one simple
> example. By applying this to any method, you're telling the rest of the
> world that the method is now considered obsolete. If you now call this
> method in your program for instance, a compiler can detect that the method
> is obsolete and display a warning for you. Or perhaps someone wants to write
> a utility that will display all obsolete methods being called from your
> program simply by reading the assembly itself (since the attribute info is
> stored there). And what if you want to add your own information about some
> entitry, say, which developer wrote a particular method for instance. You
> can create your own "MethodAuthorAttribute" that takes the developers's
> employee # (or some other appropriate ID) and this can appplied to every
> method in your program. The attrbute can then be used for any number of
> reasons. Perhaps you want to dump this to a log at runtime if an unexpected
> exception is thrown (for diagnostic purposes). The code can determine which
> method was involved and then dump the author's ID out by reading that
> method's "MethodAuthorAttribute". Your help staff would then be able to
> quickly determine which developer originally wrote the function (or maybe
> your company's QA staff uses it during internal testing only). This example
> is contrived of course but you get the idea. Have a look at the "Inheritance
> Hierarchy" section under the "Attribute" class in MSDN
> (http://msdn.microsoft.com/en-us/libr...attribute.aspx). These are
> the native .NET attributes and many are used for obscure purposes but they
> still fulfill their role, namely, they provide information about the
> entities they apply to (to those who need it).


Thank you very much.
 
Reply With Quote
 
Jeff Johnson
Guest
Posts: n/a
 
      13th Apr 2009
"CSharper" <(E-Mail Removed)> wrote in message
news:07c87d92-8a34-42f4-92a7-(E-Mail Removed)...

> I have seen lot of programs uses attributes.
>
> [Attribute]
> public class method()
> {
> }
>
> I know attribute is nothing but a class derived from Attribute. I
> still couldn't wrap my head around when to use and what it does during
> run time.


For the most part (in my experience), attributes have very little to do with
RUN time and much more to do with design time and compile time. The
PropertyGrid control is a big consumer of attributes. You can control the
way properties in the grid behave (such as whether they display at all, or
what editor will be used for them) by decorating them with attributes.


 
Reply With Quote
 
Arne Vajhøj
Guest
Posts: n/a
 
      14th Apr 2009
CSharper wrote:
> I have seen lot of programs uses attributes.
>
> [Attribute]
> public class method()
> {
> }
>
> I know attribute is nothing but a class derived from Attribute. I
> still couldn't wrap my head around when to use and what it does during
> run time. Can some one kindly point me to a detailed place where I can
> read? Most of the examples are very simple and I don't see any use or
> need of those. If you have time, could you kindly explain me what is
> this?


The attributes of a class are typical used when some other code is
looking at the class via reflection and use the information in the
attributes to do something.

Examples:
- an XML serializer uses the attributes to decide which properties
becomes XML elements and which becomes XML attributes
- an O/R-mappers uses the the attributes to decide on table
names and column names

Arne
 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Attributes question Esha Microsoft VB .NET 2 26th Oct 2006 01:04 PM
attributes.add question Jennyfer J Barco Microsoft ASP .NET 4 20th Dec 2004 09:27 PM
Question about attributes... Girish Microsoft C# .NET 5 1st Nov 2004 08:49 PM
One more question about Attributes C.G. Oh Microsoft Dot NET Framework 2 20th Sep 2004 04:35 PM
Attributes.Add question REB Microsoft ASP .NET 2 29th Jan 2004 08:58 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 10:38 PM.