Custom Attributes

  • Thread starter Thread starter mark
  • Start date Start date
M

mark

Could someone please give me a practical example of how you HAVE used custom
attributes ... not just how you COULD use them. I understand the syntax,
but am not able to visualize a good use for them.

Thanks in advance.

Mark
 
Mark,

I've used them extensively on enumeration values, used in a class
factory pattern (I use the enumeration value as the key). It definitely is
a better way to store comples static information, IMO. This way, you don't
have to go through the mess of populating your data structures and creating
the backing store for them.

That's just one use. I've used them to provide interception-like
capabilities, as well as in plug in architectures (to indicate how a type
will fit into the architecture, and provide some default capabilities).

There are plenty of uses out there for attributes. It's really a great
feature to have in the framework, since the uses are pretty vast.

Hope this helps.
 
mark said:
Could someone please give me a practical example of how you HAVE used custom
attributes ... not just how you COULD use them. I understand the syntax,
but am not able to visualize a good use for them.

I wrote a program for my former employer which generated install scripts
to add our framework components to the Global Assembly Cache and to
register in COM+ those which needed it. I created custom attributes with
which to mark assemblies to indicate which server the components were to
be installed upon and whether they were to be installed in COM+.
 
I agree with Nicholas, it's a fantastic feature to have in the framework

As another example, very early on in my .NET development, I created a
generic data access layer that used attributes to map properties and fields
of a class to a table and columns of a table. It's not really the most
efficient way to handle it (reflection can be expensive, in terms of
performance), but it was a good solution at the time.

When I first started doing C# programming and I read about attributes and
reflection, I thought, "Well, that sounds kind of neat, but I don't really
see how useful it could be." Now I don't know how I ever got by without it.

Pete


Nicholas Paldino said:
Mark,

I've used them extensively on enumeration values, used in a class
factory pattern (I use the enumeration value as the key). It definitely
is a better way to store comples static information, IMO. This way, you
don't have to go through the mess of populating your data structures and
creating the backing store for them.

That's just one use. I've used them to provide interception-like
capabilities, as well as in plug in architectures (to indicate how a type
will fit into the architecture, and provide some default capabilities).

There are plenty of uses out there for attributes. It's really a great
feature to have in the framework, since the uses are pretty vast.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

mark said:
Could someone please give me a practical example of how you HAVE used
custom attributes ... not just how you COULD use them. I understand the
syntax, but am not able to visualize a good use for them.

Thanks in advance.

Mark
 
Back
Top