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
6
votes
3 answers

Troubleshooting PostSharp license issue on build server: Where can I find my license for PostSharp Express?

I am experiencing issues with my CI build server in which PostSharp is causing the build to fail with the message: The assembly 'yada-yada.dll' uses non-licensed features (Basic Features). [...]. We are using the current version of PostSharp…
Vinney Kelly
  • 4,975
  • 1
  • 25
  • 31
6
votes
3 answers

Suppressing PostSharp Multicast with Attribute

I've recently started experimenting with PostSharp and I found a particularly helpful aspect to automate implementation of INotifyPropertyChanged. You can see the example here. The basic functionality is excellent (all properties will be…
Dan Bryant
  • 27,329
  • 4
  • 56
  • 102
6
votes
1 answer

Postsharp: How to set the return value after an exception

Using Postsharp, how do you set the return value after an exception has been thrown? I thought this would work: namespace MvcApplication3.Controllers { public class ValuesController : ApiController { // GET api/values/5 …
Ev.
  • 7,109
  • 14
  • 53
  • 87
6
votes
3 answers

PostSharp 2.0 BadImageFormatException

We have an application here which is using postsharp to wrap certain methods within a transaction aspect derived from MethodInterceptionAspect. We use NHibernate 2.0 as an ORM for the application. There is a failure within this block of code,…
intervigil
  • 101
  • 1
  • 5
6
votes
3 answers

Is there a way in Log4Net or NLog (or some other logger) to output logs in an execution-stack-nested XML or JSON format?

Is there a way in Log4Net or NLog (or some other logger) to output logs in an execution-stack-nested XML or JSON format, such that if function A() calls B(7) that calls C("something"), it'll output something like:
Tar
  • 8,529
  • 9
  • 56
  • 127
6
votes
1 answer

Cannot resolve dependency to assembly 'PostSharp' because it has not been preloaded

My project structure A.dll, which has PostSharp installed via NuGet and uses it. B.exe, which references A and does not use PostSharp. Edit: I created a test solution to see if I could recreate the issue, and the error went away, so it appears to…
Kendall Frey
  • 43,130
  • 20
  • 110
  • 148
6
votes
2 answers

Is it possible to use PostSharp 3 with Visual Studio Express?

PostSharp 3 is available only as a visual studio extension. Visual Studio Express don't support extension. Is there a way to use PostSharp 3 with Express version of Visual Studio? Previously we use PostSharp 2.1 as an external tools with free…
Danila Polevshchikov
  • 2,228
  • 2
  • 24
  • 35
6
votes
1 answer

Apply an aspect to only methods that have a specific attribute

I'm trying to set up a PostSharp aspect RunOutOfProcessAttribute so that it applies to: all public methods any method marked with the DoSpecialFunctionAttribute, regardless of member accessibility (public/protected/private/whatever). So far, my…
Fabian Tamp
  • 4,416
  • 2
  • 26
  • 42
6
votes
1 answer

What is the reason I get "Non abstract, non-.cctor-method in an interface"?

I have a very strange problem. I have an Interface defined in a dll as follows: public interface IKreator2 { string Name { get; set; } string Description { get; set; } INotifyPropertyChanged Settings { get; set; } InfiniRenderJob…
f0rt1s
  • 396
  • 1
  • 3
  • 12
6
votes
2 answers

How to continue method flow using OnException aspect (PostSharp)?

I have the following code: [Serializable] class ExceptionAspectHandler:OnExceptionAspect { public override void OnException(MethodExecutionArgs args) { Console.WriteLine("{0}", args.Exception); …
Gustavo Puma
  • 993
  • 12
  • 27
5
votes
2 answers

Disable Postsharp in debug builds for entire solution

I have a solution with many projects and I would like to disable Postsharp for debug builds to reduce local developer build times. Is there any way to do this without editing each project file? I know this may sound like a bad idea, but we are only…
Rob Bird
  • 3,764
  • 2
  • 28
  • 38
5
votes
1 answer

Is it possible to select which projects PostSharp processes instead of telling it which to exlude?

I can specify the SkipPostSharp constant to ensure a project is excluded from the list of projects PS processes. I want to do it the other way around though. I want PS to assume it shouldn't process anything that I don't specifically tell it…
Brandon Moore
  • 8,590
  • 15
  • 65
  • 120
5
votes
1 answer

Usage of AspectPriority

I'm using PostSharp 2.1.5.1 and had a warning today: Aspect dependencies (defined on "MyNamespace.MyAspect.MyVerificationAttribute") will be disabled from the Starter Edition in future versions. Use the AspectPriority property instead. Seems…
Akim
  • 8,469
  • 2
  • 31
  • 49
5
votes
1 answer

how to apply a Postsharp aspect solution wide (all classes in namespace)

I am trying to modify the sample trace app that ships with Postsharp so that the trace is applied to all classes in my namespace without explicitly putting the [QuickTrace] on top of each class. I have attached a screenshot. What am I doing wrong ?…
Gullu
  • 3,477
  • 7
  • 43
  • 70
5
votes
0 answers

PostSharp support on M1 Macs (linux-arm64)

I've recently started using PostSharp for caching on a .NET 5 app and it's working phenomenally on my 2015 MacBook Pro (Intel). However, I just recently got an M1 Mac Mini, which has Apple's own M1 chipset (linux-arm64), and, unfortunately, I am not…
Marshal
  • 51
  • 2