/src/LinFu.AOP/TypeWeaverVisitor.cs

http://github.com/philiplaureano/LinFu · C# · 50 lines · 31 code · 7 blank · 12 comment · 3 complexity · 2e45ca5392c5c849b65fa8b72619164f MD5 · raw file

  1. using System.Collections.Generic;
  2. using LinFu.AOP.Cecil.Interfaces;
  3. using Mono.Cecil;
  4. namespace LinFu.AOP.Cecil
  5. {
  6. /// <summary>
  7. /// Represents a visitor class that can iterate over <see cref="TypeDefinition" />
  8. /// instances.
  9. /// </summary>
  10. public class TypeWeaverVisitor : BaseReflectionVisitor
  11. {
  12. private readonly HashSet<ModuleDefinition> _visitedModules = new HashSet<ModuleDefinition>();
  13. private readonly ITypeWeaver _weaver;
  14. /// <summary>
  15. /// Initializes a new instance of the TypeWeaverVisitor class.
  16. /// </summary>
  17. /// <param name="weaver">The <see cref="ITypeWeaver" /> that will be used to modify a given type.</param>
  18. public TypeWeaverVisitor(ITypeWeaver weaver)
  19. {
  20. _weaver = weaver;
  21. }
  22. /// <summary>
  23. /// Visits a <see cref="TypeDefinition" /> instance.
  24. /// </summary>
  25. /// <param name="type">A <see cref="TypeDefinition" /> object.</param>
  26. public override void VisitTypeDefinition(TypeDefinition type)
  27. {
  28. if (type.IsEnum)
  29. return;
  30. if (!_weaver.ShouldWeave(type))
  31. return;
  32. var module = type.Module;
  33. if (!_visitedModules.Contains(module))
  34. {
  35. _weaver.ImportReferences(module);
  36. _weaver.AddAdditionalMembers(module);
  37. _visitedModules.Add(module);
  38. }
  39. _weaver.Weave(type);
  40. base.VisitTypeDefinition(type);
  41. }
  42. }
  43. }