/src/LinFu.AOP/NewOperatorInterception/ImplementActivatorHostWeaver.cs

http://github.com/philiplaureano/LinFu · C# · 62 lines · 48 code · 13 blank · 1 comment · 8 complexity · 73a7b69a621e2f2b63cb3a547b45cffe MD5 · raw file

  1. using System;
  2. using LinFu.AOP.Cecil.Interfaces;
  3. using LinFu.AOP.Interfaces;
  4. using LinFu.Reflection.Emit;
  5. using Mono.Cecil;
  6. namespace LinFu.AOP.Cecil
  7. {
  8. internal class ImplementActivatorHostWeaver : ITypeWeaver
  9. {
  10. private readonly Func<TypeReference, bool> _filter;
  11. private TypeReference _activatorPropertyType;
  12. private TypeReference _hostInterfaceType;
  13. public ImplementActivatorHostWeaver()
  14. : this(type => true)
  15. {
  16. }
  17. public ImplementActivatorHostWeaver(Func<TypeReference, bool> filter)
  18. {
  19. _filter = filter;
  20. }
  21. public bool ShouldWeave(TypeDefinition item)
  22. {
  23. if (item.Name == "<Module>")
  24. return false;
  25. if (!item.IsClass || item.IsInterface)
  26. return false;
  27. if (_filter != null)
  28. return _filter(item);
  29. return true;
  30. }
  31. public void Weave(TypeDefinition type)
  32. {
  33. // Implement IActivatorHost only once
  34. if (type.Interfaces.Contains(_hostInterfaceType))
  35. return;
  36. type.AddProperty("Activator", _activatorPropertyType);
  37. if (!type.Interfaces.Contains(_hostInterfaceType))
  38. type.Interfaces.Add(_hostInterfaceType);
  39. }
  40. public void AddAdditionalMembers(ModuleDefinition module)
  41. {
  42. _hostInterfaceType = module.ImportType<IActivatorHost>();
  43. _activatorPropertyType = module.ImportType<ITypeActivator>();
  44. }
  45. public void ImportReferences(ModuleDefinition module)
  46. {
  47. }
  48. }
  49. }