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
4
votes
4 answers

Free analogue of the PostSharp required

I have heard of PostSharp, but I am wondering if there are any other similar tools - anything analogous to PostSharp? Are there any libraries that could be used as an alternative to PostSharp? Any other tools that can post-process an assembly and…
Igor Soloydenko
  • 11,067
  • 11
  • 47
  • 90
4
votes
1 answer

Log4PostSharp for PostSharp 2.0 and .NET framework 2.0

I have a C# project targeting .NET framework 2.0. I also want to use PostSharp 2.0 Community Edition + Log4PostSharp. The problem is that it's not possible to use Log4PostSharp because it targets 3.5 framework. Also it's not possible to change…
ReVolly
  • 126
  • 8
4
votes
1 answer

IoC with AOP (PostSharp) in MonoDroid

I'm working on a MonoDroid app, and there really isn't a good DI solution yet (at least that I know of). I've gotten PostSharp to work on MonoDroid, and I'm using the Location Intercept aspect as a way to inject dependencies into fields/properties…
Matthew Groves
  • 25,181
  • 9
  • 71
  • 121
4
votes
1 answer

Postsharp MethodInterceptionAspect get method return value

I have an implementation of MethodInterceptionAspect(PostSharp) but when I in the override OnInvoke method, the args.Method is null, I need to know the method return value type, anyone know about? [PSerializable] public class…
Shoshana Tzi
  • 99
  • 4
  • 10
4
votes
1 answer

postsharp express limited to 10 target classes per project

Does it apply to any aspect? Is that it? If I use a multicast attribute it will apply randomly to the first 10 matches only? I'm not sure if I got this restriction right. Do you mind elaborating it a little? Examples would be well welcome'd.
hugo00
  • 117
  • 1
  • 9
4
votes
1 answer

Why is postsharp causing a web project to fail on publish?

In version 4.2.27 of postsharp publishing a web project works correct. When the postsharp nuget package is upgraded to > 4.2.28 the publish fails. It fails when trying to run TransformWebConfigCore in Microsoft.Web.Publishing.targets. The…
4
votes
2 answers

Adding an OnException attribute using PostSharp

I am adventuring into some AOP and it seems with .NET PostSharp is the way to go. I want to do some simple logging to the db when an exception occurs. However I am finding it difficult to find any real solid examples of using PostSharp beyond the…
Chris James
  • 11,571
  • 11
  • 61
  • 89
4
votes
3 answers

How to get current NHibernate session for using Transaction attribute using PostSharp AOP?

I want to get current NHibernate Session for using the Transaction attribute using PostSharp AOP. Instead of below, public void Create(TEntity entity) where TEntity : class, IIdentity { var session = SessionFactory.CurrentSession; …
Ninad Naik
  • 43
  • 1
  • 6
4
votes
1 answer

How to cast object to method return type

I want to set args.ReturnValue to instance of an object created from TResponse method called Create. [Serializable] public sealed class LogError : OnMethodBoundaryAspect { public override void OnException(MethodExecutionArgs args) { …
msmolcic
  • 6,407
  • 8
  • 32
  • 56
4
votes
0 answers

Unhandled exception (4.1.15.0, postsharp.4.0-x86.exe, CLR 4.0.30319.378758, Release): PostSharp.Sdk.CodeModel.AssemblyLoadException

I have updated PostSharp from v 4.1.3-alpha to v 4.1.15. There is no issue when I build on my local developer box, but when queueing a gated build it's failing on build server with the following error: Unhandled exception (4.1.15.0,…
Rajanikar
  • 41
  • 5
4
votes
1 answer

Get base class Member of controller in OnEntry in OnMethodBoundaryAspect in PostSharp

I want access base class member in our Log Aspect Class. I have one base controller & that controller inherit by Test controller & in Test Controller i implemented AOP Aspect. In BaseContoller i have a member _userSession. I initializing…
virender
  • 4,539
  • 5
  • 27
  • 31
4
votes
0 answers

'Add PostSharp to this project' no action

I added PostSharp via NuGet (PostSharp.4.0.40, but I also checked the .39 version), created a class inheriting from OnExceptionAspect (e.g. CustomExceptionBehaviour - I tested previously this aspect working for me in a sample Console application),…
aly
  • 373
  • 2
  • 7
  • 22
4
votes
2 answers

Get parameter name

How do you get the parameter NAMES of a method. The examples show you how to get the values of the parameters, but not the NAMES. I want to see parma = 99, parmb = 1. Not just 99, 1. using System; using System.Collections.Generic; using…
dannyrosalex
  • 1,794
  • 4
  • 16
  • 25
4
votes
2 answers

Implement interfaces based on class properties without reflection

This page on the PostSharp website has the following teaser: One of the common situations that you will encounter is the need to implement a specific interface on a large number of classes. This may be INotifyPropertyChanged, IDispose, IEquatable…
Matthew Strawbridge
  • 19,940
  • 10
  • 72
  • 93
4
votes
2 answers

Can Spring.Net function as PostSharp?

A few months back I've discovered PostSharp, and for a time, it was good. But then legal came back with an answer saying that they don't like the licence of the old versions. Then the department told me that 2.0's price was unacceptably high (for…
Alex K
  • 10,835
  • 8
  • 29
  • 34