/examples/LinFu.AOP.Samples/LinFu.AOP.MethodInterceptionSample/Program.cs

http://github.com/philiplaureano/LinFu · C# · 49 lines · 34 code · 9 blank · 6 comment · 2 complexity · 3edb64cd550ef9779f8d93a9020b1981 MD5 · raw file

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Reflection;
  5. using System.Text;
  6. using System.Threading;
  7. using LinFu.AOP.Cecil.Extensions;
  8. using LinFu.AOP.Interfaces;
  9. using SampleLibrary;
  10. namespace LinFu.AOP.MethodInterceptionSample
  11. {
  12. public class SampleInterceptor : IInterceptor
  13. {
  14. public object Intercept(IInvocationInfo info)
  15. {
  16. var methodName = info.TargetMethod.Name;
  17. Console.WriteLine("method '{0}' called", methodName);
  18. // Replace the input parameter with 42
  19. var result = info.TargetMethod.Invoke(info.Target, new object[]{42});
  20. return result;
  21. }
  22. }
  23. // NOTE: Remember to check the SampleLibrary.csproj file for an example on how to use LinFu.AOP.Tasks to transparently
  24. // postweave your DLLs!
  25. class Program
  26. {
  27. static void Main(string[] args)
  28. {
  29. var employee = new Employee();
  30. // All LinFu.AOP-modified objects implement the IModifiableType interface
  31. var modifiableType = employee as IModifiableType;
  32. // Plug in our custom implementation
  33. if (modifiableType != null)
  34. modifiableType.MethodBodyReplacementProvider = new SimpleMethodReplacementProvider(new SampleInterceptor());
  35. // The employee object will call the interceptor instead of the actual method implementation
  36. employee.Pay(12345);
  37. return;
  38. }
  39. }
  40. }