Home Page

Welcome to PostSharp!

PostSharp is a tool that can reduce the number of lines of code in all major Microsoft .NET languages and improve its logical decoupling. Therefore its helps you delivering higher stability, cleaner design, and cheaper source code maintenance.

Have you already implemented things like transaction management, logging, caching, or security? In each method, you had to write a same dozen of lines of code, right?

Thanks to PostSharp, you can encapsulate these aspects as custom attributes. This is sometimes called aspect-oriented programming (AOP) or policy injection. Look out:

{loadposition inset}

And if you have 5 minutes left, look at this video. If you are already sold, download the software and start killing redundancies in your code today.

Getting Started With PostSharp

PostSharp Laos makes aspect-oriented programming easy. You can start writing your own aspect in a matter of minutes. Just follow the guide!

1. Before your forget...

Bookmark us with and subscribe to our RSS feed.

2. Download & Install PostSharp

Yes, you have to download and install! But don't worry, it's only 5 MB. Execute the installer (you will need administrative rights) and restart Visual Studio.

3. Open Your Favorite Project

Load one of your projects into Visual Studio 2005 or higher. Any project type will work, but for this example better choose a console application. No matter if you use C#, VB.NET, or another language: it will just work. (Since ASP.NET Web Sites don't use MSBuild, it's a little more difficult with these projects; refer to documentation for details.)

4. Add References To PostSharp

You want to use PostSharp in your project, isn't it? So add a reference to two assemblies: PostSharp.Public.dll and PostSharp.Laos.dll.

Add References to PostSharp

5. Create a New Aspect Class

Create a class and name it TraceAttribute.

public sealed class TraceAttribute : Attribute
 private readonly string category;
public TraceAttribute( string category )
this.category = category;
public string Category { get { return category; } }

6. Derive the Class from OnMethodBoundary

Includes the namespace PostSharp.Laos and make the new class inherit from OnMethodBoundaryAspect. Since this last class derives from System.Attribute, you are actually developing a custom attribute.

public sealed class TraceAttribute : OnMethodBoundaryAspect

7. Implement the Handlers

Once the aspect custom attribute will be applied to a method, this method will invoke the aspect handlers. Do we want to trace methods? So just add trace logic to the handlers!

    public override void OnEntry( MethodExecutionEventArgs eventArgs )
string.Format( "Entering {0}.{1}.",
eventArgs.Method.Name ),
this.category );
public override void OnExit( MethodExecutionEventArgs eventArgs )
string.Format( "Leaving {0}.{1}.",
eventArgs.Method.Name ),
this.category );

8. Apply the Custom Attribute to Methods

In your project, choose an existing class you want to log and decorate it with our new custom attributes. All methods will be traced. If you prefer to trace some of these methods, don't apply the custom attribute on the class, but on each method.

internal static class Program
private static void Main()
Trace.Listeners.Add(new TextWriterTraceListener( Console.Out));

[Trace( "MyCategory" )]
private static void SayHello()
Console.WriteLine("Hello, world." );
private static void SayGoodBye()
Console.WriteLine("Good bye, world.");

And what if you have a, say, very large assembly? You surely don't want to decorate every method to log, don't you? Well, no problem at all!

[assembly: Trace( "MyCategory", AttributeTargetTypes = "My.BusinessLayer.*")] 


9. We're done! Compile & Execute

That's all, really! If you have a console application, the output should include tracing information.

The output of our program with tracing.

10. How did it work?

Seems magic, huh? Well, to some extent, it is. Open the Output Window of Visual Studio and look at the Build Output: you will see that PostSharp has been invoked. This is the magic wand!

Build output: PostSharp is inserted in the build process.

And now, inspect your assembly using .NET Reflector. You will see how PostSharp enhanced your code:

More tutorials like this

This tutorial is a shorter version of a two-parts article published on A good way to continue reading about PostSharp!


 PostSharp 1.0PostSharp 1.5
Microsoft .NET 2.0, 3.0, 3.5YesYes
Novell Mono (experimental) Yes
Silverlight 2 Yes
Compact Framework 2.0 Yes
C# (all versions)YesYes
Visual Basic (all versions)YesYes
ASP.NET Web Sites Yes
Seamless MSBuild IntegrationYesYes
Command-Line UtilityYesYes
NAnt Task Yes
Extensive Debugging ExperienceYesYes
Windows InstallerYesYes
Deployment in Source RepositoryYesYes
Pay-as-you-use ComplexityYesYes
Aspects are plain C#/VB classesYesYes
Declarative Build-Time ValidationYesYes
Imperative Build-Time ValidationYesYes
Build-Time InitializationYesYes
Aspect Inheritance Yes
Pluggable Aspect Serialization Yes
Laos Aspect Types  
Around Method YesYes
On ExceptionYesYes
Intercept Method YesYes
Intercept FieldYesYes
Implement Abstract/Extern MethodYesYes
Add InterfaceYesYes
Compound AspectsYesYes
GPL/LGPL License AvailableYesYes
Commercial License AvailableYesYes