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

Post sharp using instance member

I am attempting to create an aspect to manage security on a few properties of a class. However, the security aspect for one member relies on the data in another property of the class. I've read some tutorials on the IntroduceAspect, but I'm not sure…
Robert Smith
  • 385
  • 4
  • 15
3
votes
3 answers

PostSharp has no effect on speed

I have stumbled on an impossibly good performance behaviour with PostSharp. To evaluate the speed I wrote a little program, that would execute one function a specified number of times, and if PostSharp is enable it would generate and delete a few…
Alex K
  • 10,835
  • 8
  • 29
  • 34
3
votes
2 answers

Possible to get previous version with NuGet?

I've started using NuGet to install PostSharp into my projects and this is what I routinely run into: 1) Start new solution and project 2) Add existing project(s) (that new project references) that already use(s) PostSharp 3) Use NuGet to add…
BVernon
  • 3,205
  • 5
  • 28
  • 64
3
votes
1 answer

Generic Decorator in C#

I wonder since there is no way how to implement a generic Decorator class in C# (is it?) like this: public class Decorator : TDecoratorInterface { public TDecoratorInterface Component {get; private set;} protected…
John Smith
  • 1,783
  • 6
  • 22
  • 36
3
votes
0 answers

Azure Worker Role + PostSharp + TFS Online

I am using PostSharp 3 Ultimate in my Azure Worker role project. I am using tfs.visualstudio.com for the source control. Now, PostSharp doesn't like NuGet Package Restore. It requires us to restore all nuget packages before we build the project on…
Moon
  • 33,439
  • 20
  • 81
  • 132
3
votes
1 answer

Apply a PostSharp aspect to all methods in class to Log method name

I would like to create an attribute, that I want to apply to a class. In this class, if a method was called, in the OnEntry and OnExit methodof postsharp, I want to log the exact method name like this: "GetPartners starting..." or "GetPartners…
Concware
  • 267
  • 1
  • 6
  • 14
3
votes
1 answer

Override base class PostSharp aspect in derived class

I have a generic repository class with various methods marked with a PostSharp aspect (SecuredOperation)... public class Repository : IRepository, ISecurable where TEntity : class, IEntity { ... …
Brett Postin
  • 11,215
  • 10
  • 60
  • 95
3
votes
3 answers

Reduce PostSharp compile time overhead

We recently introduced PostSharp into our code base and the compile time of our ASP.NET MVC project has doubled to quadrupled. We have about 3 MVC projects and approximately 8 class library projects in our solution. Obviously there will be overhead…
Jason
  • 4,232
  • 3
  • 23
  • 31
3
votes
1 answer

Get the project path/output path during Postsharp compile time

Is it possible to find out the project folder/project file or output path of the assembly during compile time in Postsharp, i.e. during CompileTimeInitialize for example, when the assembly is being built?
Anupheaus
  • 3,383
  • 2
  • 24
  • 30
3
votes
1 answer

Has anybody used any AOP products under Mono?

I'm looking into using PostSharp on one of my projects. The complicating factor is that I need PostSharp to work on both Windows x64 and Linux x64/Mono. According to the available info for PostSharp, you can compile on Windows/.NET and run under…
kmontgom
  • 1,419
  • 13
  • 18
3
votes
1 answer

How to exclude from OnMethodBoundaryAspect-based logging?

I have this logger: [Serializable] [AttributeUsage(AttributeTargets.All)] public class MethodsInterceptAspect : OnMethodBoundaryAspect { public override void OnEntry(MethodExecutionArgs args) { Logger.LogMethodEntry(args.Method, DateTime.Now);…
Tar
  • 8,529
  • 9
  • 56
  • 127
3
votes
1 answer

Refactoring nasty legacy systems via AOP or other automated means?

I've recently been playing around with PostSharp, and it brought to mind a problem I faced a few years back: A client's developer had produced a web application, but they had not given a lot of thought to how they managed state information - storing…
Andrew Matthews
  • 3,006
  • 2
  • 29
  • 42
3
votes
1 answer

Why is PostSharp skipping one of my projects on the build server?

I upgraded PostSharp recently via NuGet and it has stopped working, but only in very select cases. It doesn't work on my build server (TeamCity), and it doesn't work on only one of my projects. The other projects all transform correctly. What…
ladenedge
  • 13,197
  • 11
  • 60
  • 117
3
votes
3 answers

How do you make Postsharp testable

Dependency injection doesn't work in Postsharp. How do I make aspects testable? I really want to avoid creating concrete classes for logging in my aspects. I don't want the aspects running when I'm unit testing methods. If I'm testing a method I…
big_tommy_7bb
  • 1,257
  • 2
  • 21
  • 37
3
votes
3 answers

How do you disable PostSharp when running unit tests?

I want my nunit tests not to apply any of my PostSharp aspects so I can test my methods in isolation. Can this be done somehow in the Test Fixture Setup, or can it only be done on a per project level?
JMac
  • 523
  • 1
  • 5
  • 17