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

msbuild uses the wrong assembly name

I've had trouble building some of the C# projects in a solution I've never had problems with. Building fails with the error that a metadata file is missing. The error description and the msbuild log output at diagnostic level shows some surprising…
Drew
  • 169
  • 1
  • 1
  • 11
5
votes
3 answers

How to get the top most type of an instance variable?

Say I have an instance variable that is declared to be a given base class. I want to find what the original, upper-most type that object actually is instead of the base class. How do I do this? I have a validation attribute a la PostSharp so the…
Bigsby
  • 952
  • 6
  • 20
5
votes
1 answer

PostSharp 4.3.21: Compiled Images Do Not Install

I am encountering a problem with PostSharp 4.3.21 where I am selecting to install compiled images but they do not actually install. Because of this, I am getting prompted if I would like to install every hour, even though I am choosing to…
Mike-E
  • 2,477
  • 3
  • 22
  • 34
5
votes
0 answers

Postsharp and Fody Compatibility

I am using the free edition of PostSharp extensively in a project. I would like to use the PropertyChanged.Fody addin to handle all the PropertyChanged Notifications automatically. (I know that PostSharp offers a library for this, but it is not…
Michal Steyn
  • 341
  • 2
  • 5
5
votes
1 answer

Postsharp OWIN Version Mismatch

I'm using PostSharp 4.2.22.0 and Owin 3.0.1 in my project. When I compile I get the following error: Unhandled exception (4.2.22.0, postsharp-net40-x86-srv.exe, CLR 4.0.30319.394271, Release): PostSharp.Sdk.CodeModel.AssemblyLoadException:…
Pinzi
  • 293
  • 6
  • 18
5
votes
4 answers

How to exclude specific type from json serialization

I am logging all requests to my WCF web services, including the arguments, to the database. This is the way I do it: create a class WcfMethodEntry which derives from PostSharp's aspect OnMethodBoundaryAspect, annotate all WCF methods with…
sventevit
  • 4,766
  • 10
  • 57
  • 89
5
votes
1 answer

PostSharp Conflicting Aspects warning

I'm using PostSharp Express in VS2013 to create validation aspects which I can apply to my properties. I followed this PostSharp guide on location interception. They all work well but I am getting hundreds of warnings stating: Conflicting aspects…
08Dc91wk
  • 4,254
  • 8
  • 34
  • 67
5
votes
1 answer

PostSharp How to know at runtime if a certain aspect was applied to a method?

I'm implementing a PostSharp aspect library and can't find out a solution to the following problem. Suppose we have an aspect that will be applied for some methods and won't be applied for the others. I need some mechanism that I can use at runtime…
Alexander Abakumov
  • 13,617
  • 16
  • 88
  • 129
5
votes
4 answers

Postsharp 3rd party class

I need decorate all method from class in 3rd party DLL. I use C# 5.0 and postsharp 3.1. Of course I can do something like this. //In 3rd party library class A { public virtual int foo(string a) {} public virtual void foo2() {} } //In…
5
votes
1 answer

How to exit a method in the OnEntry method of a PostSharp aspect based on condition

I'd like the aspect to exit a method invocation based on a condition like the following: [AttributeUsage(AttributeTargets.Method)] public class IgnoreIfInactiveAttribute : OnMethodBoundaryAspect { public override void…
Michael Ulmann
  • 1,077
  • 5
  • 16
5
votes
2 answers

Postsharp OnException Aspect not Working as Expected

I have the following custom aspect, and have tried applying it at project and class level. In all cases, even an intentional divide by zero, the OnException method is never called. What am I doing wrong? [Serializable] public class…
ProfK
  • 49,207
  • 121
  • 399
  • 775
5
votes
4 answers

Why is post-compilation code injection a better idea than pre-compilation code injection?

So we all know that C# doesn't have a C-like macro pre-processor (and there's a good thread on why here). But now that AOP is gaining traction, it seems like we're starting to do stuff with post-processors that we used to do with pre-processors…
dkackman
  • 15,179
  • 13
  • 69
  • 123
5
votes
1 answer

Can Reflection be used in CompileTimeInitialize in PostSharp 3.1?

Is it possible to use reflection in CompileTimeInitialize in PostSharp 3.1? Following code worked in 3.0: public class TestClass { public string TestField; [TestAspect] public void TestMethod() { } } public class TestAspect :…
Peter Han
  • 547
  • 1
  • 6
  • 13
5
votes
1 answer

Explain HOW the MVC Authorize Attribute performs AOP-like actions

I've been trying to figure out how this works on a low-level: [Authorize] public ActionResult Index() { return View(); } Basically, the above code snippet seems to intercept calls to the Index method, perform an authorization check, and throw…
Michael
  • 1,535
  • 2
  • 17
  • 32
5
votes
1 answer

Applying aspects to aspects with postsharp

I am looking into using post sharp and I am trying to put together a few demos. One of them was an aspect that will log exceptions, and another was to check for null arguments and throw an exception when one is encountered. The problem I am having…
James Considine
  • 323
  • 2
  • 13