|
| 1 | +using System; |
| 2 | +using System.IO; |
| 3 | +using System.Reflection; |
| 4 | +using PostSharp.Aspects; |
| 5 | +using PostSharp.Reflection; |
| 6 | + |
| 7 | +namespace CSharpExamples |
| 8 | +{ |
| 9 | + [Serializable] |
| 10 | + public class DeltaAspect : LocationInterceptionAspect |
| 11 | + { |
| 12 | + //public static StreamWriter Writer = new StreamWriter(@"c:\deltaaspect.txt"); |
| 13 | + |
| 14 | + private static Action<string> log = (text) => |
| 15 | + { |
| 16 | + // Writer.WriteLine(text); |
| 17 | + // Writer.Flush(); |
| 18 | + Console.WriteLine(text); |
| 19 | + }; |
| 20 | + public override void OnSetValue(LocationInterceptionArgs args) |
| 21 | + { |
| 22 | + object oldValue = args.GetCurrentValue(); |
| 23 | + object newValue = args.Value; |
| 24 | + |
| 25 | + if (args.Location.LocationKind == LocationKind.Property) |
| 26 | + { |
| 27 | + if (args.Location.PropertyInfo.PropertyType.IsValueType && |
| 28 | + !args.Location.Name.EndsWith("__BackingField")) |
| 29 | + { |
| 30 | + if (!oldValue.Equals(newValue)) |
| 31 | + { |
| 32 | + var classInstance = args.Instance; //this is the class who's property changed |
| 33 | + var text = $"{args.LocationFullName} changed {oldValue} -> {newValue}"; |
| 34 | + log(text); |
| 35 | + } |
| 36 | + } |
| 37 | + } |
| 38 | + |
| 39 | + base.OnSetValue(args); |
| 40 | + } |
| 41 | + } |
| 42 | + |
| 43 | + [Serializable] |
| 44 | + public sealed class TraceAspect : OnMethodBoundaryAspect |
| 45 | + { |
| 46 | + //public static StreamWriter Writer = new StreamWriter(@"c:\methodaspect.txt"); |
| 47 | + |
| 48 | + static Action<string> log = (text) => |
| 49 | + { |
| 50 | + // Writer.WriteLine(text); |
| 51 | + // Writer.Flush(); |
| 52 | + Console.WriteLine(text); |
| 53 | + }; |
| 54 | + |
| 55 | + [NonSerialized] private string enteringMessage; |
| 56 | + [NonSerialized] private string exitingMessage; |
| 57 | + |
| 58 | + public TraceAspect() { } |
| 59 | + |
| 60 | + public override void RuntimeInitialize(MethodBase method) |
| 61 | + { |
| 62 | + var methodName = method.DeclaringType.FullName + "." + method.Name; |
| 63 | + enteringMessage = "Method Enter: "+ methodName; |
| 64 | + exitingMessage = "Method Exit: " + methodName; |
| 65 | + } |
| 66 | + |
| 67 | + public override void OnEntry(MethodExecutionArgs args) => log(enteringMessage); |
| 68 | + |
| 69 | + public override void OnExit(MethodExecutionArgs args) => log(exitingMessage); |
| 70 | + } |
| 71 | + [DeltaAspect] |
| 72 | + [TraceAspect] |
| 73 | + class ClassIWantToTrack |
| 74 | + { |
| 75 | + public int Number { get; set; } |
| 76 | + public void Dog() { } |
| 77 | + public void Cat() { } |
| 78 | + } |
| 79 | + |
| 80 | + public static partial class Examples |
| 81 | + { |
| 82 | + public static void Example3() |
| 83 | + { |
| 84 | + Console.WriteLine("Example3"); |
| 85 | + ClassIWantToTrack foo = new ClassIWantToTrack(); |
| 86 | + foo.Number = 1; |
| 87 | + foo.Number = 2; |
| 88 | + foo.Dog(); |
| 89 | + foo.Cat(); |
| 90 | + |
| 91 | + //prints: |
| 92 | + |
| 93 | + //Method Enter: CSharpExamples.ClassIWantToTrack..ctor |
| 94 | + //Method Exit: CSharpExamples.ClassIWantToTrack..ctor |
| 95 | + //Method Enter: CSharpExamples.ClassIWantToTrack.set_Number |
| 96 | + //CSharpExamples.ClassIWantToTrack.Number changed 0 -> 1 |
| 97 | + //Method Exit: CSharpExamples.ClassIWantToTrack.set_Number |
| 98 | + //Method Enter: CSharpExamples.ClassIWantToTrack.set_Number |
| 99 | + //CSharpExamples.ClassIWantToTrack.Number changed 1 -> 2 |
| 100 | + //Method Exit: CSharpExamples.ClassIWantToTrack.set_Number |
| 101 | + //Method Enter: CSharpExamples.ClassIWantToTrack.Dog |
| 102 | + //Method Exit: CSharpExamples.ClassIWantToTrack.Dog |
| 103 | + //Method Enter: CSharpExamples.ClassIWantToTrack.Cat |
| 104 | + //Method Exit: CSharpExamples.ClassIWantToTrack.Cat |
| 105 | + |
| 106 | + } |
| 107 | + } |
| 108 | +} |
0 commit comments