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
11
votes
1 answer

PostSharp: Custom attributes are removed when using OnMethodInvocationAspect

I've got some aspect like this: public class MyAttribute : OnMethodInvocationAspect { public int Offset { get; internal set; } public MyAttribute(int offset) { this.Offset = offset; } public override void…
Jan Jongboom
  • 26,598
  • 9
  • 83
  • 120
10
votes
3 answers

PostSharp - il weaving - thoughts

I am considering using Postsharp framework to ease the burden of application method logging. It basically allows me to adorn methods with logging attribute and at compile time injects the logging code needed into the il. I like this solution as it…
redsquare
  • 78,161
  • 20
  • 151
  • 159
10
votes
1 answer

How to modify method arguments using PostSharp?

I need to do some stuff with parameteres passed into my method. How can I play with them (modify) using PostSharp ?
Tony
  • 12,405
  • 36
  • 126
  • 226
10
votes
1 answer

How do you reflect on an attribute applied to a return value?

Consider the following: [AttributeUsage(AttributeTargets.Parameter | AttributeTargets.ReturnValue)] public class NotNullAttribute : Attribute { } public class Class1 { [return: NotNull] public static string TestMethod([NotNull] string arg) …
user47589
10
votes
4 answers

How to inject/generate plumbing code into methods decorated with an Attribute?

I was reading through some articles on Caching and Memoization and how to implement it easily using delegates and generics. The syntax was pretty straightforward, and it is surprisingly easy to implement, but I just feel due to the repetitive nature…
Yannick Motton
  • 34,761
  • 4
  • 39
  • 55
9
votes
3 answers

pdb file is mising after PostSharp

I am using PostSharp version 2.1.6.4 (also tried latest version 2.1.7.35) and sometimes pdb file is missing and there is a pssym file in it's place.
Aleksei Poliakov
  • 1,322
  • 1
  • 14
  • 27
9
votes
2 answers

Can I restrict a custom attribute to void methods only?

I have a custom attribute which I would like to restrict to methods with return type void. I know I can restrict to methods using [AttributeUsage(AttributeTargets.Method)] but there doesn't seem to be a way to restrict the return type or any other…
Roman Reiner
  • 1,089
  • 1
  • 10
  • 17
9
votes
3 answers

issue with PostSharp cannot find assembly for system.web.mvc, version=3.0.0.0 when no projects reference it

I'm using PostSharp, and this was working fine. Recently, we upgraded some projects to the MVC version 5.2. Today, I loaded up an old project which was using version 4.0 of MVC. PostSharp started reporting a strange error on build: Error 17 …
Karl Cassar
  • 6,043
  • 10
  • 47
  • 84
8
votes
1 answer

PostSharp OnMethodBoundaryAspect OnEntry Not Executing

I am running a .NET 4.0 Web Application (not web site) and PostSharp 1.5. I cannot get the OnEntry override method to execute using the OnMethodBoundaryAspect base class. Here is some relevant code: public sealed class MonitorAttribute :…
a432511
  • 1,907
  • 4
  • 26
  • 48
8
votes
1 answer

How to uninstall Postsharp or change it's licence when there is no Visual Studio installed?

I have installed Postsharp on my build server. There were only Microsoft Build Tools (MSBuild) without Visual Studio installed. Now I need to change the license of the postsharp. When I run the installer, it says that the postsharp is already…
Heavy
  • 1,861
  • 14
  • 25
8
votes
1 answer

Filtering log4net on method name - can't quite get it

I'm using log4net to log my web app's progress, using Log4PostSharp to AOP-injectify all methods. This has the desired effect of logging (almost) everything and is fine. I now have a requirement to log JUST Page_Load methods to a file / console. I…
Mike Kingscott
  • 477
  • 1
  • 5
  • 19
8
votes
2 answers

Assembly wide multicast attributes. Are they evil?

I am working on a project where we have several attributes in AssemblyInfo.cs, that are being multicast to methods of a particular class. [assembly: Repeatable( AspectPriority = 2, AttributeTargetAssemblies = "MyNamespace", AttributeTargetTypes =…
Egor Pavlikhin
  • 17,503
  • 16
  • 61
  • 99
8
votes
2 answers

Applying an attribute to an interface using PostSharp

I want to be able to apply an attribute to an interface so that every method in any class that implements that interface will have the attribute applied to it. I assumed it would look something like…
krisg
  • 796
  • 1
  • 9
  • 24
8
votes
4 answers

Why use a post compiler?

I am battling to understand why a post compiler, like PostSharp, should ever be needed? My understanding is that it just inserts code where attributed in the original code, so why doesn't the developer just do that code writing themselves? I expect…
Robert MacLean
  • 38,975
  • 25
  • 98
  • 152
8
votes
2 answers

AOP vs MVC FilterAttributes vs Interceptors

ASP.NET MVC proposes that use or extend built-in Authorization, Action, Result, Exception filters. 3th party .Net IoC containers (Unity, Ninject, Autofac) propose Interceptors 3th party AOP tools (Postsharp) propose their attributes. Now, I'm…
Nuri YILMAZ
  • 4,291
  • 5
  • 37
  • 43
1
2
3
61 62