Questions tagged [postsharp]

PostSharp is a pattern-aware extension to C# and VB, reducing boilerplate code involved in implementing design patterns. It is based on aspect-oriented programming and static analysis.

With PostSharp, you can easily write and apply custom attributes that add new behaviors to your code - tracing, thread management, exception handling, data binding, and much more.

PostSharp works by injecting IL during the build process to weave aspects into the original method.

Ready-made patterns

PostSharp comes with a library of ready-made pattern implementations including code contracts, INotifyPropertyChanged, immutable, freezable and other threading models.

Example: the following snippet shows how the NotifyPropertyChanged attribute automatically implementes the INotifyPropertyChanged interface, including listening to two levels of child objects.

[NotifyPropertyChanged]
public class CustomerViewModel
{
    public CustomerModel Customer { get; set; }

    public string FullName
    {
        get
        {
            if (Customer == null) return null;

            return string.Format("{0} {1} from {2}",
                Customer.FirstName,
                Customer.LastName,
                Customer.PrincipalAddress != null ? 
                  Customer.PrincipalAddress.FullAddress : "?");
        }
    }
}

Custom patterns

PostSharp also has a rich toolkit to automate the implementation of your own patterns.

Examples:

Tracing:

[PSerializable]
public class TraceAttribute : OnMethodBoundaryAspect
{
  public override void OnEntry(MethodExecutionEventArgs eventArgs)
  { 
     Trace.TraceInformation("Entering {0}.", eventArgs.Method); 
  }

  public override void OnExit(MethodExecutionEventArgs eventArgs)
  { 
     Trace.TraceInformation("Leaving {0}.", eventArgs.Method); }
  }
}

Thread dispatch WPF:

[PSerializable]
public class GuiThreadAttribute : OnMethodInvocationAspect
{
   public override void OnInvocation(MethodInvocationEventArgs eventArgs)
   {
       DispatcherObject dispatcherObject = (DispatcherObject)eventArgs.Delegate.Target;
       if (dispatcherObject.CheckAccess())
           eventArgs.Proceed();
       else
           dispatcherObject.Dispatcher.Invoke(DispatcherPriority.Normal,
                                               new Action(() => eventArgs.Proceed()));
   }
}

Usage:

[Trace]
public void CreateCustomer(int id, string name) { /* ... */ }

Installation

PostSharp can most easily be installed through its NuGet package.

Install-Package PostSharp

Licensing

PostSharp is a commercial product with a free edition.

916 questions
3
votes
4 answers

Aspect Oriented Programming: What do you use PostSharp for?

I would like to ask users of the AOP framework Postsharp, what specifically are you using the framework for? Also, I know it's use has a big negative impact on build times, but how about runtime performace? Is there much of a hit? Thanks, S
UpTheCreek
  • 31,444
  • 34
  • 152
  • 221
3
votes
2 answers

Common.Logging with PostSharp and NLog 2.0

I use Common.Logging as a wrapper around NLog 2.0. I've done this so that I can replace NLog with another logging provider in the future. I also use PostSharp to not write a try catch block everytime I need one. I have a class that inherits the…
Jurgen Vandw
  • 631
  • 8
  • 27
3
votes
1 answer

Error when compiling c# project in VS2012 when using Postsharp

I am currently working on a project where we were wanting to add PostSharp functionality. I have set up my Postsharp attribute as so [Serializable] public class NLogTraceAttribute : OnMethodBoundaryAspect { private readonly string _logLevel; …
Thewads
  • 4,953
  • 11
  • 55
  • 71
3
votes
2 answers

Is there a way to use PostSharp to make constructor parameters visible at the class level?

Say I have a lot of boilerplate code like this: class MyClass { private readonly IDependencyA dependencyA; private readonly IDependencyB dependencyB; public MyClass( IDependencyA dependencyA, IDependencyB dependencyB) …
Scott Whitlock
  • 13,739
  • 7
  • 65
  • 114
3
votes
1 answer

PostSharp intercept class constructor and destructor calls

Is it possible to intercept class constructor and destructor calls using PostSharp? I would like to create active instances counter for classes.
Tomas
  • 17,551
  • 43
  • 152
  • 257
3
votes
1 answer

PostSharp OnMethodBoundaryAspect Not Thread Safe

I'm trying out PostSharp AOP and am surprised that OnMethodBoundaryAspect is not thread safe. The same instance of the aspect is shared between method calls. This makes its utility quite limited in number of use cases where it can be applied. Any…
AVP06
  • 1,079
  • 1
  • 10
  • 16
3
votes
2 answers

Are typed/generic aspects worth the effort?

I've done a little research on typed/generic aspects. An important fact about aspects is obliviousness. So the concerns of the aspects should be orthogonal to the domain concerns. Nevertheless there are investigations to make AspectJ type safe…
Matthias
  • 15,919
  • 5
  • 39
  • 84
3
votes
1 answer

How to write a PostSharp Invoke aspect to simplify cross thread control updates

When I want to update controls across threads this is how I generally end up doing it: this.Invoke((MethodInvoker)delegate { SomeProcedure(); }); The suggested way to do it is actually to call the invoker for the specific control you want to…
Brandon Moore
  • 8,590
  • 15
  • 65
  • 120
3
votes
1 answer

Logging to multiple log files with Postsharp and log4net

I have a system that get data (usually exe files and text files) from several service provider, and my system get this data and preform some work on it (basically analyse its behavior). I'm using Log4Net to generate log files and I would like to…
doubleM
  • 133
  • 4
  • 15
2
votes
2 answers

How Can an Attribute Class Reference the Instance that Uses It?

Let's say I have an attribute class: public class MyCustomAttribute : Attribute { // do stuff } And I use this attribute on class properties: public class MyModel : BaseModel { [MyCustom] public string Name { get; set; } } Is there a…
David
  • 208,112
  • 36
  • 198
  • 279
2
votes
1 answer

In PostSharp is it possible to modify the value of a single argument to a method?

My current method can restrict operations to operate on strings, but I need finer grain control. I want to do things like, set elements to title case which would only be applicable to some params, but for this I would need to be able to operate on a…
gbro3n
  • 6,729
  • 9
  • 59
  • 100
2
votes
1 answer

How to resolve log dependency from postsharp aspect using dependency injection

so I am putting together a project using NLog with Postsharp. I have a OnMethodBoundaryAspect attribute descendant to decorate classes and provide for intercepted execution for logging. My DI container is Unity. So, using DI alone (not…
lstutzman
  • 23
  • 1
  • 2
  • 3
2
votes
1 answer

Postsharp - Adding OnMethodBoundaryAspect to abstract Method - Aspect Not Firing

I'm trying to implement an OnMethodBoundary aspect on an abstract method in an abstract class so that all types that inherit from this class will automatically have the aspect applied. There are no compilation errors or warnings, but the OnEntry…
Marcus
  • 111
  • 1
  • 1
  • 10
2
votes
1 answer

Postsharp and log4net and log4postsharp

I stumbled upon log4postsharp site which is a great tool that uses postsharp for injecting log4net statements into your code at compile time. The current version of log4postsharp uses Postsharp 1.0 which has some limitations. Does anyone know if…
Fadeproof
  • 3,038
  • 5
  • 31
  • 42
2
votes
1 answer

Using Postsharp to introduce a constructor

I have some dependencies inside of my aspects, and I'd like use an IoC container to manage the lifecycle of these dependencies. My first thought is that introducing a constructor which takes one more argument than the most specific constructor in…
Khanzor
  • 4,830
  • 3
  • 25
  • 41