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

is it possible to apply an aspect to an assembly without adding a reference in that assembly to the aspect?

I wrote a an aspect called [NLogMethods] that logs method boundry to nlog, based on the example from Postsharp website. I'd like to apply it to multiple DLLs in my infrastructure, for use in some of my applications. in order to do this - I need to…
user1127788
  • 151
  • 1
  • 1
  • 4
2
votes
1 answer

PostSharp - How to inject a method into a class?

I have some problem with PostSharp (I assume that problem can be solved by other library). I have that scenario: I must get all classes with name containing a word, e.g "Sth". I have that classes in my project (an…
Tony
  • 12,405
  • 36
  • 126
  • 226
2
votes
1 answer

Why is DbgView missing some trace writes, but the traces can be seen in test runners

Can anyone explain why DbgView misses some of my trace writes ? I'm using the Enterprise Library 5.0 logging block with a trace listener deriving from the EntLib CustomTraceListener, as below ...…
SteveC
  • 15,808
  • 23
  • 102
  • 173
2
votes
1 answer

PostSharp Logging Add >>> and <<< to Method Entry/Exit Log Entries

Can someone please tell me if it's possible to add >>> and <<< to the log entries made by PostSharp Logging at method entry and exit? If it's possible, can someone please help get me started? Google has not been a friend and PostSharp's…
user17055229
2
votes
1 answer

PostSharp Pointcuts

Before I start, I'd like to clarify that my current understanding of AOP terminology is as follows... Aspects are the AOP equivalent of Classes in OOP. Advices are the AOP equivalent of Methods in OOP. Pointcuts are the AOP equivalent of 'using'…
Lawrence Wagerfield
  • 6,471
  • 5
  • 42
  • 84
2
votes
1 answer

Which are the most suitable languages to apply Aspect's Theme approach?

I am thinking about reading Aspect-Oriented Analysis and Design: The Theme Approach, yet I am hesitant. Is it possible to use what's taught in the book with AspectJ (for Java) or Post# in C#? Maybe with Aquarium in Ruby? What would be the perfect…
devoured elysium
  • 101,373
  • 131
  • 340
  • 557
2
votes
2 answers

PostSharp won't build in latest Visual Studio (16.8.1)

Since upgrading to the latest Visual Studio (16.8.1), I can no longer compile any projects that use PostSharp. I receive the following build errors (for PostSharp v6.7.11 AND for 6.8.1-preview): 1>POSTSHARP : error : error: Unhandled exception…
Ian
  • 41
  • 4
2
votes
1 answer

PostSharp Tools for Visual Studio 2019 cannot be installed in Visual

Setting up Postsharp has been a big problem for me. Can't install 2019 VS postsharp? PostSharp Tools for Visual Studio 2019 cannot be installed in Visual Studio Community 2019 (16.7.1) because the following prerequisites are missing: Just In Time…
zafer k
  • 43
  • 1
  • 6
2
votes
4 answers

How do you give a C# Auto-Property a default value using a custom attribute?

How do you give a C# Auto-Property a default value, using a custom attribute? This is the code I want to see: class Person { [MyDefault("William")] public string Name { get; set; } } I am aware that there is no built in method to…
Contango
  • 76,540
  • 58
  • 260
  • 305
2
votes
1 answer

Postsharp throws System.Runtime.InteropServices.COMException (0x8013141C): Strong name key container not found. (Exception from HRESULT: 0x8013141C)

Adding the question for anyone in the future that needs to resolve the same problem Environment: Visual Studio 2017 or 2019 Build error from Postsharp signing the assembly MSBuild postsharptest.csproj /T:Rebuild /P:PostSharpTrace=StrongName /V:D…
2
votes
0 answers

PostSharp on Mac

I want to use postSharp on Visual Studio for Mac. I downloaded postSharp package on Nuget. But my onEntry Aspect didnt work ? Is there any document about how to install postSharp macOS or how to build postSharp on visual studio for mac ? Have a…
Fatih AKSOY
  • 106
  • 1
  • 10
2
votes
1 answer

AOP Postsharp, log the variables value

With postsharp is there a way from attribute to get variable value. This attribute will write some logs in database or nlog [AOPTattribute($"The value of 'myint' is {myInt}")] public void MyMethod() { int myInt = (default) int; /* some…
TheBoubou
  • 19,487
  • 54
  • 148
  • 236
2
votes
0 answers

ASP net core API logging via Postsharp

I am implementing AOP based logging using Postsharp. I am following https://doc.postsharp.net/add-logging but this does not enlist all information . This is what I have done so far to use Postsharp with loupe [Loupe is capturing information without…
Dmehro
  • 1,269
  • 1
  • 16
  • 29
2
votes
1 answer

Validate class names in multiple projects at once using PostSharp

I have a solution with several API projects in various namespaces. I'm looking for a way to validate all the API projects at the same time, to check if any of them has a class (controller) with the same name as another class in any of those…
2
votes
2 answers

Change code based on existing attribute?

I'm writing a library which is shared between .net and silverlight. I have several places where I am doing this, to satisfy the silverlight deserialization (which can't access private members): [DataMember (IsRequired = true)] public Object…
Sam Holder
  • 32,535
  • 13
  • 101
  • 181