PageRenderTime 101ms CodeModel.GetById 60ms app.highlight 3ms RepoModel.GetById 29ms app.codeStats 0ms

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