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

Postsharp tracing on/off in runtime?

Can anyone tell me, if in PostSharp, I somehow can turn on/off tracing in runtime? I need to write less code, so lately I could simply remove it. Tracing functionality is temporarily required. Maybe there's an alternative to PostSharp with runtime…
GaaRa
  • 520
  • 6
  • 21
4
votes
1 answer

Call alternative method in class using postsharp

I want to be able to call a differnt method on my intercepted class by using PostSharp. Say I have the following method in my PostSharp aspect: public override void OnInvoke(MethodInterceptionArgs args) { if…
Lee Cook
  • 573
  • 4
  • 18
3
votes
4 answers

Using PostSharp to intercept calls to Silverlight objects?

I'm working with PostSharp to intercept method calls to objects I don't own, but my aspect code doesn't appear to be getting called. The documentation seems pretty lax in the Silverlight area, so I'd appreciate any help you guys can offer :) I have…
Gabriel Isenberg
  • 25,869
  • 4
  • 37
  • 58
3
votes
0 answers

Postsharp using generic interfaces

Not sure if this is possible, but is there a way to access my generic interface when using LocationInterceptionAspect without using reflection? I want to cast the current instance of an interface type (which is generic) and execute methods. I am…
BoredOfBinary
  • 793
  • 3
  • 12
  • 20
3
votes
3 answers

How to implement Lazy loading with PostSharp?

I would like to implement lazy loading on properties with PostSharp. To make it short, instead of writing SomeType _field = null; private SomeType Field { get { if (_field == null) { _field = LongOperation(); …
remio
  • 1,242
  • 2
  • 15
  • 36
3
votes
1 answer

PostSharp - Apply aspect across assemblies

I have several projects and wish to apply aspects across multiple projects. I use the multicast attribute like so: [assembly:MyProject.Aspects.NotifiableObject( AttributeTargetAssemblies = "MyProject.Entities", AttributeTargetTypes =…
Tri Q Tran
  • 5,500
  • 2
  • 37
  • 58
3
votes
1 answer

Can PostSharp compile time weaving introduce problems?

I was reading about PostSharp, And I see that it uses compile-time weaving. I know that PostSharp is a pretty mature, but is its weaving system safe enough for commercial use ? Has anyone ever encountered problems, or knows of problems that can be…
Yochai Timmer
  • 48,127
  • 24
  • 147
  • 185
3
votes
0 answers

signing dll and postsharp

I’ve got a problem with postsharp on my hudson CI server. Whenever I try to sign a dll with specific version an error occurs. (Hudson is using MSBuild from cmd to build this project , building it as x86) : POSTSHARP : postsharp error PS0099:…
vert
  • 31
  • 1
3
votes
3 answers

PostSharp: initialize instance-scoped aspect after target constructors

I have written an aspect implementing IInstanceScopedAspect and inheriting from LocationInterceptionAspect. When initialized, it reads some properties from the target object. The problem is that IInstanceScopedAspect.RuntimeInitializeInstance() is…
Daniel Wolf
  • 12,855
  • 13
  • 54
  • 80
3
votes
1 answer

Postsharp Newbie - Why is args.Instance null?

New to PostSharp --- I'm trying out the NuGet version now and I'm trying to understand wny in the AuthoriseAttribute OnEntry method that the agrs.Instance value is null. I'm trying to implement authorsation that depends on the values of the object…
3
votes
1 answer

PostSharp and Blazor

I'm looking at using PostSharp on a Blazor server project and was wondering if the limitation/restrictions outlined in this Postsharp blog (https://blog.postsharp.net/post/blazor-support-6.7.html) apply equally to both the client and server versions…
Mike
  • 45
  • 4
3
votes
1 answer

How to call Postsharp Aspect in Unit Test?

I have an aspect sorting list. public override void OnInvoke(MethodInterceptionArgs args) { args.Proceed(); var string_list = (args.ReturnValue as List); string_list.Sort(); Console.WriteLine("Postsharp Aspect içerisinde…
ImC0der
  • 308
  • 2
  • 18
3
votes
1 answer

Postsharp does not log on trace level

I like to log some Postsharp messages on trace level. Unfortunately logs to this level print no output. All other levels are working. Same behavior with console or NLog backend or when I log from an other class. How can I enable the trace…
r2d2
  • 592
  • 5
  • 15
3
votes
2 answers

PostSharp vs DynamicProxy2 Interface Interception

I've used PostSharp in the past to do AOP and I've been checking out AOP using Autofac and DynamicProxy2 and I'm curious if there are any benefits of one of the other. i.e. Is one more reliable, testable, stable, performant, etc, etc than the…
Adam
  • 3,014
  • 5
  • 33
  • 59
3
votes
1 answer

PostSharp displays call stack as "Aspect Code" and makes call stack less usable

On certain breakpoints in my code, the call stack is displayed as "Aspect Code", and I can't use the call stack window to identify or navigate up the call stack. When does this occur and why? I uninstalled PostSharp and this went away. Is there a…
Hoppe
  • 6,508
  • 17
  • 60
  • 114