PageRenderTime 39ms CodeModel.GetById 22ms RepoModel.GetById 1ms app.codeStats 0ms

/src/LinFu.AOP/ImplementMethodReplacementHost.cs

http://github.com/philiplaureano/LinFu
C# | 57 lines | 44 code | 12 blank | 1 comment | 8 complexity | 33bda347afebe788a44dda9cff54de52 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 ImplementMethodReplacementHost : ITypeWeaver
  9. {
  10. private readonly Func<TypeReference, bool> _filter;
  11. private TypeReference _hostType;
  12. public ImplementMethodReplacementHost(Func<TypeReference, bool> filter)
  13. {
  14. _filter = filter;
  15. }
  16. public virtual bool ShouldWeave(TypeDefinition item)
  17. {
  18. if (item.Name == "<Module>")
  19. return false;
  20. if (!item.IsClass || item.IsInterface)
  21. return false;
  22. // Implement the host interface once and only once
  23. if (item.Interfaces.Contains(_hostType))
  24. return false;
  25. if (_filter != null)
  26. return _filter(item);
  27. return true;
  28. }
  29. public virtual void Weave(TypeDefinition item)
  30. {
  31. if (item.Interfaces.Contains(_hostType))
  32. return;
  33. item.Interfaces.Add(_hostType);
  34. item.AddProperty("MethodBodyReplacementProvider", typeof(IMethodReplacementProvider));
  35. item.AddProperty("MethodCallReplacementProvider", typeof(IMethodReplacementProvider));
  36. }
  37. public virtual void AddAdditionalMembers(ModuleDefinition host)
  38. {
  39. }
  40. public virtual void ImportReferences(ModuleDefinition module)
  41. {
  42. _hostType = module.Import(typeof(IMethodReplacementHost));
  43. }
  44. }
  45. }