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

NotifyPropertyChanged on dependent properties

I have the following view model [NotifyPropertyChanged] public class ActivateViewModel { public string Password { get; set; } public bool ActivateButtonEnabled { get { return !string.IsNullOrEmpty(Password); } } ... } In my view I'm…
The Muffin Man
  • 19,585
  • 30
  • 119
  • 191
3
votes
0 answers

Postsharp update problems in common packages

We had a common project where we had exception handling. And using Postsharp sounded smart. The thing is now it is an obstacle in our daily Nuget updates. Fixing the version number is also fishy as we want the latest versions. And we can not…
3
votes
1 answer

How to apply exception aspect in PostSharp with multiple return statements in method?

I am using PostSharp aspects for exception handling inside of Controller in ASP.NET MVC project. Simplified code is like this: [ExceptionAspect(AspectPriority = 1)] public ActionResult MarkeSettings() { try{ SaveData(); …
coder
  • 658
  • 3
  • 14
  • 31
3
votes
2 answers

PostSharp & classes that (are supposed to) implement INotifyPropertyChanged

I'm new to PostSharp and am trying to figure out whether it can make my life implementing property changed events a bit easier. However, I'm facing the first issue already. I have my interfaces in one Project library, interfaces.dll: public…
Bogey
  • 4,926
  • 4
  • 32
  • 57
3
votes
1 answer

PostSharp NotifyPropertyChanged Model - PropertyChangedEventHandler

I have litle problem with PostSharp Implementation of INotifyPropertyChanged. PostSharp added PropertyChangedEventHandler PropertyChanged after compile time, but I need react from C# too. Model a = new Model(); a.PropertyChanged +=…
Peter
  • 33
  • 3
3
votes
2 answers

PostSharp OnMethodBoundaryAspect

I'm working on an aspect with postsharp 1.5 and OnMethodBoundaryAspect. I want my aspect have the following behavior by default: 1-If the attribute is used at class level the aspect is applied only on PUBLIC methods. 2-The user of the aspect can…
José F. Romaniello
  • 13,866
  • 3
  • 36
  • 38
3
votes
1 answer

Configuring Postsharp logging toolkit

Using the free version of Postsharp, I added a logging aspect (using the toolkit, didn't code it myself). Later I changed my mind and wanted to log only upon entering a function, and not on leaving. Where can this be configured? Couldn't find it…
Nir
  • 3,963
  • 8
  • 37
  • 51
3
votes
1 answer

PostSharp injecting dependency to aspect

Is it possible to inject dependency into PostSharp aspect? I'd like to create NavigateBackAspect - something like that: [PSerializable] class NavigateBackAspect : OnMethodBoundaryAspect { private readonly INavigationService _navigationService; …
fex
  • 3,488
  • 5
  • 30
  • 46
3
votes
2 answers

Implementing INotifyPropertyChanged with PostSharp 1.5

Im new to .NET and WPF so i hope i will ask the question correctly. I am using INotifyPropertyChanged implemented using PostSharp 1.5: [Serializable, DebuggerNonUserCode, AttributeUsage(AttributeTargets.Assembly | AttributeTargets.Class,…
no9
  • 125
  • 1
  • 2
  • 7
3
votes
1 answer

Automatic property validation

Sometimes, I have really complex models with many string properties that need to be validated on setting, however the validation doesn't often go further than IsNotNullOrWhitespace. This often leads to unnecessary code repetition, so I wondered if…
Acrotygma
  • 2,531
  • 3
  • 27
  • 54
3
votes
1 answer

Ignore a property with INotifyPropertyChanged in PostSharp

When applying PostSharp's [INotifyPropertyChanged] attribute to a class, is there a way to force PostSharp to ignore some of the properties?
Mau
  • 14,234
  • 2
  • 31
  • 52
3
votes
2 answers

Code protection and code weaving in .net

I tried to use code protection (code is encrypted and can't be reflected) made by clisecure with postsharp but secured dlls won't compile when post sharp is used in solution. I use just PostSharp.Laos and PostSharp.Public Have You ever tried such…
PK.
  • 588
  • 1
  • 4
  • 12
3
votes
2 answers

Declaratively Adding Code Contracts (maybe with Postsharp?)

I would like to add a Contract.Requires to every method in my code that has a parameter of a certain type. How would I achieve this? Consider the following trivialized example: public class ValuesController : ApiController { public string…
Ev.
  • 7,109
  • 14
  • 53
  • 87
3
votes
1 answer

Replace Attribute constructor arguments with mono.cecil or postsharp

I have an example method definition: [FooAttribute("One","time")] public void Bar(){} Is it possible through one of the above techniques to change, for example, the argument "one" to "two" ?
Sergiu Todirascu
  • 1,367
  • 15
  • 23
3
votes
1 answer

PostSharp and aspect inheritance through hierarchy

According to PostSharp documentation, aspect inheritance should be among others supported on: Parameter or Return Value of an abstract, virtual or interface method But in my case it doesn't seem to support inheritance when those supported elements…
Ladislav Mrnka
  • 360,892
  • 59
  • 660
  • 670