PageRenderTime 22ms CodeModel.GetById 19ms app.highlight 1ms RepoModel.GetById 1ms app.codeStats 0ms

/src/LinFu.AOP/MethodCallFilterAdapter.cs

http://github.com/philiplaureano/LinFu
C# | 43 lines | 21 code | 4 blank | 18 comment | 1 complexity | 5b442397f0165f32bf2c32e34bd1b7cb MD5 | raw file
 1using System;
 2using LinFu.AOP.Cecil.Interfaces;
 3using Mono.Cecil;
 4
 5namespace LinFu.AOP.Cecil
 6{
 7    /// <summary>
 8    ///     Represents a type that converts functors into method call filter instances.
 9    /// </summary>
10    public class MethodCallFilterAdapter : IMethodCallFilter
11    {
12        private readonly Func<MethodReference, bool> _hostMethodFilter;
13        private readonly Func<MethodReference, bool> _methodCallFilter;
14
15        /// <summary>
16        ///     Initializes a new instance of the <see cref="MethodCallFilterAdapter" /> class.
17        /// </summary>
18        /// <param name="hostMethodFilter">
19        ///     The method filter that will determine the host methods that will be modified for
20        ///     interception.
21        /// </param>
22        /// <param name="methodCallFilter">The method filter that will determine which method calls will be intercepted.</param>
23        public MethodCallFilterAdapter(Func<MethodReference, bool> hostMethodFilter,
24            Func<MethodReference, bool> methodCallFilter)
25        {
26            _hostMethodFilter = hostMethodFilter;
27            _methodCallFilter = methodCallFilter;
28        }
29
30
31        /// <summary>
32        ///     Determines whether or not a particular method call should be intercepted.
33        /// </summary>
34        /// <param name="targetType">The host type that contains the method call.</param>
35        /// <param name="hostMethod">The method that contains the current method call.</param>
36        /// <param name="currentMethodCall">The method call to be intercepted.</param>
37        /// <returns>Returns <c>true</c> if the method call should be intercepted; otherwise, it will return <c>false</c>.</returns>
38        public bool ShouldWeave(TypeReference targetType, MethodReference hostMethod, MethodReference currentMethodCall)
39        {
40            return _hostMethodFilter(hostMethod) && _methodCallFilter(currentMethodCall);
41        }
42    }
43}