1 / 12

PostSharp – Aspect-Oriented Programming in C#

PostSharp – Aspect-Oriented Programming in C#. Stephen Hammack. Overview. C# is an object-oriented programming language with elements of other programming paradigms Imperative, functional, event-driven, etc. Does not include aspect-oriented programming

ilana
Download Presentation

PostSharp – Aspect-Oriented Programming in C#

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. PostSharp – Aspect-Oriented Programming in C# Stephen Hammack

  2. Overview • C# is an object-oriented programming language with elements of other programming paradigms • Imperative, functional, event-driven, etc. • Does not include aspect-oriented programming • PostSharp is a Visual Studio plugin that adds aspect-oriented programming to C# • This plugin utilizes classes and attributes to define and apply aspects • After Visual Studio compiles the program to MSIL, PostSharp runs a post-processor to apply the aspects • Only requires the PostSharp libraries at run-time

  3. OnMethodBoundaryAspect • Aspect class for injecting behaviors on methods • Four advices: OnEntry, OnExit, OnSuccess, and OnException • All advices have MethodExecutionArgsas the argument • Provides access to the intercepted method’s state and the instance it belongs to • Can change return value and alter what happens when the advice returns • Also holds an object to be passed between the advices

  4. Example Implementation

  5. Example Implementation

  6. LocationInterceptionAspect • Aspect class for injecting behaviors on properties and fields • Two advices: OnGetValue and OnSetValue • Both advices have LocationInterceptionArgs as the argument • Provides access to the value of the property or field • Can utilize proceed to run the default get/set • Note: in C#, properties can specify custom get/set methods

  7. Example Implementation

  8. Example Implementation

  9. Additional Aspect Features • Can inject behaviors to events • Events are native C# constructs for notifying observers in event-driven design • Able to introduce properties, methods, and interfaces to classes through the aspect • Mark things that are to be introduced from an aspect with the appropriate attribute

  10. Applying Aspects to Many Targets • Similar to pointcuts, PostSharp can apply aspects to many targets through multicasting, rather than using one attribute per target • Uses a different syntax than AspectJpointcuts • Assembly attributes are added outside of any class or namespace, often in its own file, to indicate what to apply the aspects to • Examples:

  11. Built-in Aspect Implementations • Non-free versions of PostSharp (and the trial) include commonly used cases for Aspect-Oriented programming • Automatic implementation of INotifyPropertyChanged • Commonly used to synchronize program model objects with the user interface • Detailed logging and tracing • Applying exception handling across the program • Simplified parameter assertion (e.g. non-null) • Many others

  12. Questions

More Related