views:

108

answers:

2

I would like to know if attributes can be used to "mix-in" functionality to a class / method / property.

Something like:

[TrackChanges]
public Foo
{
   get;
   set;
}

Does anyone how this would be implemented, if at all possible?

A: 

They certainly can but you will have to use reflection to do it. Also just because you can do it does not mean it is easy or elegant.

Reflection will give you access to every member in any type you wish. You would have to retrieve those members and work through the .NET reflection API to manipulate them. While this may work it would not be the easiest thing to maintain or read.

Andrew Hare
+3  A: 

Have a look at PostSharp, an AOP framework. It is a post-compiler, which uses custom attributes to inject additional behavior to existing code. Most of the examples are usually tracing and security.

hmemcpy