Jump to content

  • Log In with Google      Sign In   
  • Create Account

We're offering banner ads on our site from just $5!

1. Details HERE. 2. GDNet+ Subscriptions HERE. 3. Ad upload HERE.


- - - - -

Can we get a bonus week about attributes?


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
1 reply to this topic

#1 Alpha_ProgDes   Crossbones+   -  Reputation: 4692

Like
0Likes
Like

Posted 01 December 2007 - 06:57 PM

They seem used to be alot in .NET but there use seem very elusive.

Sponsor:

#2 Spoonbender   Members   -  Reputation: 1254

Like
0Likes
Like

Posted 01 December 2007 - 08:56 PM

Yes, they are elusive as hell[grin]

It took me quite a few months, but I think I've mostly got it now. (I haven't checked the C# specs though, and may be wrong on a few points)

Basically, they're just metadata you can tie to a class or method.
A few attributes are "special", and are interpreted by the compiler to make fun things happen.
One example is ObsoleteAttribute (use it by specifying [Obsolete("insert message here")] just above a class or method definition)
When the compiler sees this, it marks the class as obsolete, and every time it's used, a compiler warning is generated with the message you specified.
There's another attribute specifying that all access to a method must be synchronized, to avoid problems with multithreading in nonthread-safe code.

More generally though, they're just "labels" you can stick on pieces of your code. The compiler doesn't do anything with them, it just sticks them into the compiled MSIL code. Using System.Reflection you can retrieve these at runtime and... do with them what you like. You might use attributes to specify which database table a given class corresponds to if you're writing an ORM mapper, or... well whatever you like. Yes, they are kinda elusive, but essentially, just a way to attach various metadata to your classes or methods.




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS