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

Work without NuGet

I try upgrading my solution to nuget but I have encountered many and many problems in every operation on reference (update many times tends to make it impossible to use some packages and find non way to remove it or make it work, our reverse proxy…
gt.guybrush
  • 1,320
  • 3
  • 19
  • 48
4
votes
1 answer

AOP and Action Filters in .NET

2 Questions: Are Action Filters in MVC considered Aspect Oriented Programming (AOP)? If so, does .NET provide something similar for non MVC code (i.e. regular class library?) The context is I want to add logging to a application. Instead of…
hatcyl
  • 2,190
  • 2
  • 21
  • 24
4
votes
1 answer

How to control the generated interface GUID of a COM interface generated with AutoDual

My problem is that I have a .NET project with like 100+ classes visible to COM. Classes are published to COM using the AutoDual attribute on the class. My problem is that I would like to let the compiler auto generate the COM Interface (hence this…
4
votes
0 answers

applying aspect defined in one project, to methods in other projects of solution

I have created an aspect class inherited from OnMethodBoundaryAspect class of Postsharp. When I use the aspect in the same project where I have defined it, it works fine. But when I use the aspect in another project in my solution, the aspect would…
4
votes
2 answers

What post-compiler (or other) options are available for re-using functionality between structs?

Say I've created a struct, called Percent. Logically, it makes sense as a struct because it represents a value and should be passed by value whenever used. The struct also contains a few properties and static implicit operators. A Percent can…
user1787270
  • 348
  • 3
  • 10
4
votes
3 answers

Unit Testing with AOP/PostSharp

I'm trying to use PostSharp to implement a security aspect in order to apply method level authorisation checks in my repository layer. The concept is outlined here. However these authorisation checks are getting in the way during unit testing,…
Brett Postin
  • 11,215
  • 10
  • 60
  • 95
4
votes
2 answers

What are the functional differences between AspectJ and PostSharp?

Does anyone know what AOP features are different between AspectJ and PostSharp (yes I know they are for different languages and platforms)? I'm trying to understand what kind of things AspectJ would allow that PostSharp would not, and vice versa.
LBushkin
  • 129,300
  • 32
  • 216
  • 265
4
votes
1 answer

How to use PostSharp with MOQ?

We are trying to use PostSharp, more specifically the OnMethodInvocationAspect, to intercept the methods of a class. The code runs fine, but when testing it with MOQ, it seems to be messing up with my mocks. If I remove the aspects, all tests…
4
votes
1 answer

Using PostSharp with F# - Need documentation with working example

I have a need to capture the input and output of F# functions and decided to try using PostSharp. I was unable to find documentation and a working F# example for using PostSharp with F#. Does anyone know where I might find such?
Guy Coder
  • 24,501
  • 8
  • 71
  • 136
4
votes
4 answers

PostSharp and debugging problems?

I've made a very simple aspect, and found a problem when debugging it (see code). I set a breakpoint on the method exit, and it hits inside "entry" method actually. PostSharp 1.5, Visual Studio 2008 SP1 Is this a known bug, are there any…
skevar7
  • 995
  • 1
  • 10
  • 21
4
votes
2 answers

How to guarantee postsharp code has been injected?

This is a hypothetical question regarding the use of PostSharp. I presume that if the PostSharp portion of the build were to be missed for any reason the attributes would be ignored and the built assemblies could still run. If I implemented…
qujck
  • 14,388
  • 4
  • 45
  • 74
4
votes
1 answer

C# attributes \ avoid hard code values

Just to confirm there isn't a way to avoid hard-coded values in c# attributes right? [SomeAttribute(3+1)] public void Foo(string s) or access class members or do anything not precompiled? I now explore the great example of retry mechanism in…
user1025852
  • 2,684
  • 11
  • 36
  • 58
4
votes
1 answer

Applying aspect on constructor in c# using PostSharp

I am working on various concepts in PostSharp. Updated: This is my program class as namespace myconstructor { class Program { static void Main(string[] args) { createfolder(); streamfolder(); …
GowthamanSS
  • 1,434
  • 4
  • 33
  • 58
4
votes
1 answer

SignalR and serializing object array

I'm new to SignalR and have done a simple test hack. I wish to serialize an object array with typed objects. By default SignalR has configured the JSon.NET serializer to not provide with type information. And I found that I could register a custom…
Mario Toffia
  • 510
  • 5
  • 16
4
votes
2 answers

Postsharp: how does it work?

Following the advice got on another question of mine, I converted the code there quoted to be used with PostSharp: Attribute: [Serializable] public sealed class InitAttribute : OnMethodBoundaryAspect { public override void…
pistacchio
  • 56,889
  • 107
  • 278
  • 420