/src/LinFu.Proxy/MethodPicker.cs

http://github.com/philiplaureano/LinFu · C# · 52 lines · 30 code · 6 blank · 16 comment · 5 complexity · 5239a91a92d323c6f56e2c59e4767b85 MD5 · raw file

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Reflection;
  5. using LinFu.IoC.Configuration;
  6. using LinFu.Proxy.Interfaces;
  7. namespace LinFu.Proxy
  8. {
  9. /// <summary>
  10. /// Represents the default class implementation for the
  11. /// <see cref="IMethodPicker" /> interface.
  12. /// </summary>
  13. [Implements(typeof(IMethodPicker), LifecycleType.OncePerRequest)]
  14. internal class MethodPicker : IMethodPicker
  15. {
  16. /// <summary>
  17. /// Determines which methods can be proxied from
  18. /// the given <paramref name="baseType" /> and <paramref name="baseInterfaces" />.
  19. /// </summary>
  20. /// <remarks>By default, only public virtual methods will be proxied.</remarks>
  21. /// <param name="baseType">The base class of the proxy type currently being generated.</param>
  22. /// <param name="baseInterfaces">The list of interfaces that the proxy must implement.</param>
  23. /// <returns>A list of <see cref="MethodInfo" /> objects that can be proxied.</returns>
  24. public IEnumerable<MethodInfo> ChooseProxyMethodsFrom(Type baseType, IEnumerable<Type> baseInterfaces)
  25. {
  26. var results = new HashSet<MethodInfo>();
  27. var baseMethods = from method in baseType.GetMethods()
  28. where method.IsVirtual && !method.IsFinal && !method.IsPrivate
  29. select method;
  30. // Add the virtual methods defined
  31. // in the base type
  32. foreach (var method in baseMethods)
  33. if (!results.Contains(method))
  34. results.Add(method);
  35. var interfaceMethods = from currentInterface in baseInterfaces
  36. from method in currentInterface.GetMethods()
  37. where method.IsPublic && method.IsVirtual &&
  38. !method.IsFinal && !results.Contains(method)
  39. select method;
  40. // Add the virtual methods defined
  41. // in the interface types
  42. foreach (var method in interfaceMethods) results.Add(method);
  43. return results;
  44. }
  45. }
  46. }