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

What is IL Weaving?

I just saw Ayende's post today about PostSharp. I downloaded the code and tried it out, and I thought it was the coolest, most easy to use way to handle AOP that I've seen. In his post, Ayende says that PostSharp accomplishes it's magic via IL…
Chris Holmes
29
votes
4 answers

How do I find the return type of a method with System.Reflection.MethodBase in C#?

how do I find out the return type of a method from the MethodBase? I'm using PostSharp and trying to override the CompileTimeValidate(MethodBase method) method to make sure the attribute is applied to a method with the correct signature. Thanks,
theburningmonk
  • 15,701
  • 14
  • 61
  • 104
25
votes
6 answers

Anyone with Postsharp experience in production?

Has anyone out there used Postsharp AOP framework in a production environment? Are there any pitfalls? In order to do some logging etc, can Postsharp be used in conjunction with Log4Net? Any tutorials on using Postsharp with Web Apps and/or Log4Net…
Perpetualcoder
  • 13,501
  • 9
  • 64
  • 99
20
votes
3 answers

PostSharp and Visual Studio Code Coverage

I've recently started using PostSharp in some of my projects and have noticed an unfortunate side effect - the code coverage in all the projects its used with drops significantly. I'm guessing the reason this happens is that the analyzer sees the…
Matt Whetton
  • 6,616
  • 5
  • 36
  • 57
20
votes
1 answer

Generating a custom compile time warning C#

I'm using VS2008 and would like to create a compile time warning / error based on custom attributes on a property (if it is possible). There are two cases which interest me currently: [MyAttribute (typeof(MyClass)] Where MyClass has to implement an…
user164771
19
votes
8 answers

Determine the source of an indirect dependency on incorrect .NET Framework version

I would like to know how I can determine the source of this build error; Warning 4 The primary reference "MyNamespace.MyProject" could not be resolved because it has an indirect dependency on the .NET Framework assembly "System.Xml,…
RJ Lohan
  • 6,497
  • 3
  • 34
  • 54
16
votes
3 answers

Mono Cecil vs. PostSharp Core vs. Microsoft CCI for implementing AOP framework

Which is the better in terms of capabilities, easy of use, documentation, samples, community/support, VS integration, known implementations, long-term viability, and build speed to implement a custom AOP framework? I'll start with what I know (I…
user65199
15
votes
1 answer

How do I add arguments to PostSharp attributes?

I have a simple PostSharp logging attribute: [Serializable] public class MethodLoggingAttribute : OnMethodBoundaryAspect { private ILog _logger; public override void OnEntry(MethodExecutionEventArgs eventArgs) { _logger =…
Sailing Judo
  • 11,083
  • 20
  • 66
  • 97
15
votes
3 answers

Cool PostSharp aspects

I'm looking for interesting PostSharp aspects - anything that you found useful and wouldn't mind sharing.
Dmitri Nesteruk
  • 23,067
  • 22
  • 97
  • 166
15
votes
2 answers

Quiet down PostSharp warnings at build without skipping PostSharp

I have PostSharp included in all of my projects, as per the recommended best practices for PostSharp. However, I don't have any aspects or other transformations in some of these assemblies; they just reference other projects that do have aspects in…
David Pfeffer
  • 38,869
  • 30
  • 127
  • 202
14
votes
7 answers

Unit Testing and PostSharp

I'm wondering what the best way to do this is... I'm interested in introducing PostSharp into one of my projects, but I'm not sure how to unit test classes marked with an attribute properly. For example: public class hello { …
Alex
  • 141
  • 3
13
votes
2 answers

How to unit test PostSharp aspects?

After asking this question about implementing an aspect with PostSharp, it came to my mind that I might have to update the code of this aspect in the future, and that I did not want to take the risk of breaking everything afterwards. So, I started…
remio
  • 1,242
  • 2
  • 15
  • 36
13
votes
1 answer

Dependency injection using compile-time weaving?

I just tried to learn about PostSharp and honestly I think it's amazing. But one thing that it is difficult for me how a pure dependency injection (not service locator) cannot be done in PostSharp aspects, perhaps in my understanding as a result of…
Samuel Adam
  • 1,327
  • 4
  • 26
  • 45
12
votes
1 answer

Ordering of Postsharp Aspects execution

Ok, this may get lengthy. I am trying to do two things: I want to have a class that implements an interface by holding an instance of another class that every call is routed to. I also want to intercept all method calls and do something. Doing…
nvoigt
  • 75,013
  • 26
  • 93
  • 142
12
votes
2 answers

Whats the difference between PostSharp and Castle Dynamic Proxy?

Just wondering what the main differences are between these libraries, how they differ in features and functionality. Hoping for more information than I could find with a Google query...
John Farrell
  • 24,673
  • 10
  • 77
  • 110
1
2 3
61 62