/src/LinFu.Proxy/ProxyImplementor.cs

http://github.com/philiplaureano/LinFu · C# · 37 lines · 23 code · 3 blank · 11 comment · 1 complexity · 9b5292d9ca221e7e0186f5576e7a189c MD5 · raw file

  1. using LinFu.AOP.Interfaces;
  2. using LinFu.IoC.Configuration;
  3. using LinFu.Proxy.Interfaces;
  4. using LinFu.Reflection.Emit;
  5. using LinFu.Reflection.Emit.Interfaces;
  6. using Mono.Cecil;
  7. using CallingConventions = Mono.Cecil.MethodCallingConvention;
  8. namespace LinFu.Proxy
  9. {
  10. /// <summary>
  11. /// A class that provides the default implementation
  12. /// for the IProxy interface.
  13. /// </summary>
  14. [Implements(typeof(ITypeBuilder), LifecycleType.OncePerRequest, ServiceName = "ProxyImplementor")]
  15. internal class ProxyImplementor : ITypeBuilder
  16. {
  17. /// <summary>
  18. /// Constructs a type that implements the
  19. /// <see cref="IProxy" /> interface.
  20. /// </summary>
  21. /// <param name="module">The module that will hold the target type.</param>
  22. /// <param name="targetType">The type that will implement the <see cref="IProxy" /> interface.</param>
  23. public void Construct(ModuleDefinition module, TypeDefinition targetType)
  24. {
  25. var proxyInterfaceType = module.Import(typeof(IProxy));
  26. var interceptorType = module.Import(typeof(IInterceptor));
  27. // Implement the IProxy interface only once
  28. if (targetType.Interfaces.Contains(proxyInterfaceType))
  29. return;
  30. targetType.Interfaces.Add(proxyInterfaceType);
  31. targetType.AddProperty("Interceptor", interceptorType);
  32. }
  33. }
  34. }