/src/LinFu.AOP/Factories/AssemblyWeaverActionFactory.cs

http://github.com/philiplaureano/LinFu · C# · 43 lines · 26 code · 3 blank · 14 comment · 0 complexity · 1bd952fa02b04cb1f406d19ab1d2fcf3 MD5 · raw file

  1. using System;
  2. using LinFu.IoC.Configuration;
  3. using LinFu.IoC.Interfaces;
  4. using Mono.Cecil;
  5. namespace LinFu.AOP.Cecil.Factories
  6. {
  7. /// <summary>
  8. /// Represents a class that generates <see cref="Action{T1,T2}" /> instances
  9. /// that apply a specific method weaver (with the name given in the first delegate parameter)
  10. /// to every type in every module of an <see cref="AssemblyDefinition" /> instance.
  11. /// </summary>
  12. [Factory(typeof(Action<string, AssemblyDefinition>), ServiceName = "AssemblyWeaver")]
  13. public class AssemblyWeaverActionFactory : IFactory
  14. {
  15. /// <summary>
  16. /// Generates <see cref="Action{T1, T2}" /> instances
  17. /// that apply a specific method weaver (with the name given in the first delegate parameter)
  18. /// to every type in every module of an <see cref="AssemblyDefinition" /> instance.
  19. /// </summary>
  20. /// <param name="request">The <see cref="IFactoryRequest" /> that describes the service request.</param>
  21. /// <returns>An action delegate that will apply a specific method weaver to all the types in the given assembly.</returns>
  22. public object CreateInstance(IFactoryRequest request)
  23. {
  24. var container = request.Container;
  25. Action<string, AssemblyDefinition> result =
  26. (weaverName, assembly) =>
  27. {
  28. // Create the lambda that can modify the target types
  29. var weaveWith =
  30. (Action<string, TypeDefinition>)
  31. container.GetService("TypeWeaver", typeof(Action<string, TypeDefinition>));
  32. var mainModule = assembly.MainModule;
  33. foreach (TypeDefinition type in mainModule.Types)
  34. // Use the method weaver on the target type
  35. weaveWith(weaverName, type);
  36. };
  37. return result;
  38. }
  39. }
  40. }