C#, .Net and Azure



PostSharp is an aspect orientent framework and as such part of the Aspect Oriented Programming model (AOP).

With PostSharp you can apply aspects to your code, which will then transform your code to new code at compile time.

There are many use cases where this is very useful.

E.g. NotifyPropertyChanged, Logging, object-relation mapping, …

Before we dive into all that, you need to understand what an aspect is and what its purpose is:

In .Net you can apply aspects to almost anything: class, field, property, method, parameters, even the assembly itself.

An aspect is simply an attribute. For more details on the Attribute class and its usage, see the MSDN documentation.

The information is instance independent and is stored inside the assembly along with all the other information a program needs to run.

Chances are you probably already used some aspects like SerializableAttribute on a class to mark it (and all its properties) as serializable by the XmlSerializer or BinarySerializer.

In case of the SerializableAttribute, no extra code is generated, but xml files can be automatically constructed from your class because the XmlSerializer looks for these attributes.

PostSharp takes attributes and dynamically generates code based on them, making attributes more than just additional information.

Let’s look at the first example:


NotifyPropertyChanged is a mean to notify the UI (or other monitors) if and when a property changes its value so the UI can update itself and reflect the changes.

The pattern is commonly used in the MVVM architecture pattern.

Essentially you implement the INotifyPropertyChanged interface (which only contains one event).

You call that event whenever a property changes (the event takes an object and a string: the string must contain the name of the property that needs updating in the UI).

And there lies also the problem that PostSharp tries to fix: the event takes a string as its argument which equals the name of a property.

If you have used INotifyPropertyChanged, you probably are familar with the following code:

private int _dummy;
public int Dummy
         return _dummy;
         _dummy = value;

OnNotifyPropertyChanged being a method that calls the event from the interface (usually implemented like this in a base class for reusability):

protected void OnNotifyPropertyChanged(string str)
     if (PropertyChanged != null)
         PropertyChanged(this, new PropertyChangedEventArgs(str));

There are two problems with the code:

Let’s look how PostSharp is supposed to solve that:

public class Test
    public int Dummy { get; set; }

We will take a look at the NotifyPropertyChanged class later, first: what does it do?

It is a simply attribute that marks the class.

When compiling your code, PostSharp will take the output (dll/exe) and goes over it again, looking for PostSharp attributes.

If it finds one, it follows the instructions to generate new code and finally outputs the edited dll/exe.

In our case, the code for each property in the class would be edited in a way to call the event after setting the value. The NotifyPropertyChanged class can be viewed here.

As you can see, this is very useful, since a simple attribute is used to flag a class as implementing the INotifyPropertyChanged pattern without having to write all the boilder plate code.

Not only is this more efficient (you won’t have to write so much code), but it also is more refactoring friendly and less cluttered.

PostSharp comes as a free starter edition (limited feature set) or full featured (175€). There is also a free version for students & teachers.

The free starter edition is enough for everyone to get started and I really recommend using it, as it can take away quite some boilerplate code.

tagged as C#, Reflection,