Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>With <a href="https://www.postsharp.net/" rel="nofollow">PostSharp</a>, 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.</p> <p>PostSharp works by injecting IL during the build process to weave aspects into the original method.</p> <p><strong>Ready-made patterns</strong></p> <p>PostSharp comes with a library of ready-made pattern implementations including code contracts, INotifyPropertyChanged, immutable, freezable and other threading models.</p> <p>Example: the following snippet shows how the NotifyPropertyChanged attribute automatically implementes the INotifyPropertyChanged interface, including listening to two levels of child objects.</p> <pre><code>[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 : "?"); } } } </code></pre> <p><strong>Custom patterns</strong></p> <p>PostSharp also has a rich toolkit to automate the implementation of your own patterns.</p> <p>Examples:</p> <p>Tracing:</p> <pre><code>[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); } } } </code></pre> <p>Thread dispatch WPF:</p> <pre><code>[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(() =&gt; eventArgs.Proceed())); } } </code></pre> <p>Usage:</p> <pre><code>[Trace] public void CreateCustomer(int id, string name) { /* ... */ } </code></pre> <p><strong>Installation</strong></p> <p>PostSharp can most easily be installed through its <a href="http://www.nuget.org/packages/PostSharp/" rel="nofollow">NuGet package</a>.</p> <pre><code>Install-Package PostSharp </code></pre> <p><strong>Licensing</strong></p> <p>PostSharp is a commercial product with a free edition.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload