postsharp

Adding an OnException attribute using PostSharp

ε祈祈猫儿з 提交于 2019-12-22 08:18:49
问题 I am adventuring into some AOP and it seems with .NET PostSharp is the way to go. I want to do some simple logging to the db when an exception occurs. However I am finding it difficult to find any real solid examples of using PostSharp beyond the basics. I tried the following: [Serializable] public sealed class LogExceptionAttribute : ExceptionHandlerAspect { public override void OnException(MethodExecutionEventArgs eventArgs) { //do some logging here } } And then attaching a [LogException]

How to exclude specific type from json serialization

 ̄綄美尐妖づ 提交于 2019-12-22 07:08:11
问题 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 WcfMethodEntry attribute, in the WcfMethodEntry I serialize the method arguments to json with the JsonConvert.SerializeObject method and save it to the database. This works ok, but sometimes the arguments are quite large, for example a custom class with a

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

一世执手 提交于 2019-12-22 06:37:01
问题 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 (bear in mind that I am only getting my feet wet with PostSharp so am perhaps off base). I am a huge fan of attributes in C#, but if a pre-processor was left out for good reasons (which, as a former MFC user I still question but nevertheless accept)

Implement interfaces based on class properties without reflection

别来无恙 提交于 2019-12-21 18:47:57
问题 This page on the PostSharp website has the following teaser: One of the common situations that you will encounter is the need to implement a specific interface on a large number of classes. This may be INotifyPropertyChanged , IDispose , IEquatable or some custom interface that you have created. I'd like to write a custom aspect that implements a general version of IEquatable based on the properties of the class it's applied to (preferably at compile-time instead of by using reflection at

'Add PostSharp to this project' no action

谁说胖子不能爱 提交于 2019-12-21 18:01:06
问题 I added PostSharp via NuGet (PostSharp.4.0.40, but I also checked the .39 version), created a class inheriting from OnExceptionAspect (e.g. CustomExceptionBehaviour - I tested previously this aspect working for me in a sample Console application), how ever on any exception in the method decorated with CustomExceptionBehaviour the OnException behaviour doesn't get executed (and checking the generated assembly indeed there is no try-catch inside the method). The project build output doesn't

Disable Postsharp in debug builds for entire solution

余生长醉 提交于 2019-12-21 17:08:55
问题 I have a solution with many projects and I would like to disable Postsharp for debug builds to reduce local developer build times. Is there any way to do this without editing each project file? I know this may sound like a bad idea, but we are only using Postsharp for exception logging and our builds go through several environments for automated/manual testing (which would use a release build), so we would pick up on any potential issues at this point. If it helps, Postsharp was added to the

C# 7 Local Functions: are attributes / aspects allowed?

我怕爱的太早我们不能终老 提交于 2019-12-21 04:53:14
问题 C# 7 introduced local functions (which is great!). Suppose I have the following code: using System; using PostSharp.Aspects; namespace AspectCS7 { class Program { private static void Main() { [MyAspect] void LocalFunction() { Console.WriteLine("Hello Aspect!"); } LocalFunction(); } } [Serializable] public class MyAspect : OnMethodBoundaryAspect { public override void OnEntry(MethodExecutionArgs args) { Console.WriteLine("Entering Aspect"); } } } This code shows compile-time errors. Is it

Using PostSharp with F# - Need documentation with working example

北城以北 提交于 2019-12-18 08:49:50
问题 I have a need to capture the input and output of F# functions and decided to try using PostSharp. I was unable to find documentation and a working F# example for using PostSharp with F#. Does anyone know where I might find such? 回答1: PostSharp does not officially support F#. It may partially work, because PostSharp works at MSIL level, but there's no guarantee that everything will work, and since it is not supported, bugs will not be solved. 来源: https://stackoverflow.com/questions/14653231

What is IL Weaving?

怎甘沉沦 提交于 2019-12-17 08:45:11
问题 I just saw Ayende's post today about PostSharp. I downloaded the code and tried it out, and I thought it was the coolest, most easy to use way to handle AOP that I've seen. In his post, Ayende says that PostSharp accomplishes it's magic via IL Weaving . Now, at some abstract level I can deduce what that means, but I wanted to see if there was a more detailed answer out there. Unfortunately, for the first time in a very long time, Google came up empty for me. And so I thought this would be a

PostSharp Parameter Validation - Using RegularExpressionAttribute to find leading/trailing spaces

社会主义新天地 提交于 2019-12-13 18:32:10
问题 I'm using PostSharp 3.1 to validate parameters of properties using validation attributes. I would like to use RegularExpressionAttribute to perform the validation, which takes in a string representing the regex. I would like to throw an exception if the string has any leading or trailing whitespace, but the string may contain spaces between words. Prior to using PostSharp attributes, I performed a check like this: if(name == name.Trim()) { throw new ArgumentException("name", "Name contains