Properties, attributes and marker interfaces

B

Ben

Hi all,

I'm a little bit consfused as to when to use each of these options.
For example, if i want to decorate a class with some information,
I could decalre a const or read only field and have this information both at
run time and write time (compile time ;)), just like an attribute. And another
option is to use a marker interface to mark (decorate it).
So, when exactly should I use attributes?
I'm probably missing something here.

Thanks,
Ben
 
N

Nicholas Paldino [.NET/C# MVP]

Ben,

Attributes are used to provide additional metadata about the member that
they are applied to. Also, you can look that information up for the type at
runtime. With this, you can develop patterns of attribute usage from which
you can develop frameworks.

Take the Description attribute in the System.ComponentModel namespace.
If you apply the Description attribute to a member, it provides a
description for that member (of the type, not the instance).

The DataContract and DataMember attributes are used with the new
serialization model in .NET 3.0 to indicate what fields should be
serialized. Attributes are used in WCF to indicate what members of a
contract are exposed, and how.

In the end, an attribute is a part of the type definition, it is a piece
of data which is attached to a member of the type.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

Attribute question 3
Forcing attributes 1
Attributes and interfaces 3
Attributes question! 4
Custom attribute ? 6
WebService interfaces and Serialization 8
Interfaces & Inheritance 1
Use of Assembly Attributes 1

Top