/src/LinFu.AOP/Extensions/CecilVisitorExtensions.cs

http://github.com/philiplaureano/LinFu · C# · 86 lines · 43 code · 6 blank · 37 comment · 0 complexity · 41ec1df9df1331f23d29f1e24d6d0572 MD5 · raw file

  1. using System.Linq;
  2. using LinFu.AOP.Cecil.Interfaces;
  3. using LinFu.AOP.Interfaces;
  4. using Mono.Cecil;
  5. namespace LinFu.AOP.Cecil.Extensions
  6. {
  7. /// <summary>
  8. /// A helper class that extends Cecil to support LinFu's weaver model.
  9. /// </summary>
  10. public static class CecilVisitorExtensions
  11. {
  12. /// <summary>
  13. /// Applies the Type transformation to the target type.
  14. /// </summary>
  15. /// <param name="host">The target type</param>
  16. /// <param name="typeWeaver">The type weaver that will make the current set of modifications.</param>
  17. public static void Accept(this TypeDefinition host, ITypeWeaver typeWeaver)
  18. {
  19. typeWeaver.Weave(host);
  20. }
  21. /// <summary>
  22. /// Applies the Type transformation to the target type.
  23. /// </summary>
  24. /// <param name="host">The target type</param>
  25. /// <param name="typeWeaver">The type weaver that will make the current set of modifications.</param>
  26. public static void Accept(this AssemblyDefinition host, ITypeWeaver typeWeaver)
  27. {
  28. var module = host.MainModule;
  29. var types = module.Types.Where(typeWeaver.ShouldWeave).ToArray();
  30. foreach (var type in types)
  31. {
  32. typeWeaver.Weave(type);
  33. }
  34. }
  35. /// <summary>
  36. /// Allows a <see cref="ITypeWeaver" /> instance to traverse any <see cref="IReflectionVisitable" />
  37. /// instance.
  38. /// </summary>
  39. /// <param name="visitable">The visitable object.</param>
  40. /// <param name="typeWeaver">The type weaver.</param>
  41. public static void Accept(this IReflectionVisitable visitable, ITypeWeaver typeWeaver)
  42. {
  43. var visitor = new TypeWeaverVisitor(typeWeaver);
  44. visitable.Accept(visitor);
  45. }
  46. /// <summary>
  47. /// Allows a <see cref="ITypeWeaver" /> instance to traverse any <see cref="IReflectionStructureVisitable" />
  48. /// instance.
  49. /// </summary>
  50. /// <param name="visitable">The visitable object.</param>
  51. /// <param name="typeWeaver">The type weaver.</param>
  52. public static void Accept(this IReflectionStructureVisitable visitable, ITypeWeaver typeWeaver)
  53. {
  54. var visitor = new TypeWeaverVisitor(typeWeaver);
  55. visitable.Accept(visitor);
  56. }
  57. /// <summary>
  58. /// Allows a <see cref="IMethodWeaver" /> instance to traverse any <see cref="IReflectionVisitable" />
  59. /// instance.
  60. /// </summary>
  61. /// <param name="visitable">The visitable object.</param>
  62. /// <param name="methodWeaver">The method weaver.</param>
  63. public static void Accept(this IReflectionStructureVisitable visitable, IMethodWeaver methodWeaver)
  64. {
  65. var visitor = new MethodWeaverVisitor(methodWeaver);
  66. visitable.Accept(visitor);
  67. }
  68. /// <summary>
  69. /// Allows a <see cref="IMethodWeaver" /> instance to traverse any <see cref="IReflectionVisitable" />
  70. /// instance.
  71. /// </summary>
  72. /// <param name="visitable">The visitable object.</param>
  73. /// <param name="methodWeaver">The method weaver.</param>
  74. public static void Accept(this IReflectionVisitable visitable, IMethodWeaver methodWeaver)
  75. {
  76. var visitor = new MethodWeaverVisitor(methodWeaver);
  77. visitable.Accept(visitor);
  78. }
  79. }
  80. }