FlySwat
FlySwat

Reputation: 175583

Harnessing the power of .NET Attributes

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?

Upvotes: 2

Views: 196

Answers (2)

Igal Tabachnik
Igal Tabachnik

Reputation: 31548

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.

Upvotes: 3

Andrew Hare
Andrew Hare

Reputation: 351476

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.

Upvotes: 0

Related Questions