PageRenderTime 102ms CodeModel.GetById 60ms app.highlight 4ms RepoModel.GetById 36ms app.codeStats 0ms

/src/LinFu.AOP/Extensions/MethodBodyInterceptionExtensions.cs

http://github.com/philiplaureano/LinFu
C# | 94 lines | 53 code | 10 blank | 31 comment | 2 complexity | 6489319cdc1dfceb204c192b1086763c MD5 | raw file
 1using System;
 2using LinFu.AOP.Cecil.Interfaces;
 3using LinFu.AOP.Interfaces;
 4using Mono.Cecil;
 5
 6namespace LinFu.AOP.Cecil.Extensions
 7{
 8    /// <summary>
 9    ///     Represents an extension class that adds method body interception support to the Mono.Cecil object model.
10    /// </summary>
11    public static class MethodBodyInterceptionExtensions
12    {        
13        /// <summary>
14        ///     Intercepts all method bodies on the target item.
15        /// </summary>
16        /// <param name="target">The target to be modified.</param>
17        public static void InterceptAllMethodBodies(this TypeDefinition target)
18        {
19            target.InterceptMethodBody(m => true);
20        }       
21
22        /// <summary>
23        ///     Intercepts all method bodies on the target item.
24        /// </summary>
25        /// <param name="target">The target to be modified.</param>
26        public static void InterceptAllMethodBodies(this AssemblyDefinition target)
27        {
28            target.InterceptMethodBody(m => true);
29        }
30
31        /// <summary>
32        ///     Intercepts all method bodies on the target assembly. 
33        /// </summary>
34        /// <param name="target">The target assembly that will be modified.</param>
35        /// <param name="methodFilter">The method filter that will be used to determine which methods will be modified.</param>
36        public static void InterceptMethodBody(this AssemblyDefinition target,
37            IMethodFilter methodFilter)
38        {
39            target.InterceptMethodBody(methodFilter.ShouldWeave);   
40        }
41
42        /// <summary>
43        ///     Intercepts all method bodies on the target item.
44        /// </summary>
45        /// <param name="target">The target to be modified.</param>
46        /// <param name="methodFilter">The method filter that will determine the methods that will be modified.</param>
47        public static void InterceptMethodBody(this TypeDefinition target, IMethodFilter methodFilter)
48        {
49            target.InterceptMethodBody(methodFilter.ShouldWeave);
50        }
51
52        /// <summary>
53        ///     Intercepts all method bodies on the target item.
54        /// </summary>
55        /// <param name="target">The target to be modified.</param>
56        /// <param name="methodFilter">The method filter that will determine the methods that will be modified.</param>
57        public static void InterceptMethodBody(this AssemblyDefinition target,
58            Func<MethodReference, bool> methodFilter)
59        {
60            var typeFilter = GetTypeFilter();
61            target.Accept(new ImplementModifiableType(typeFilter));
62
63            IMethodWeaver interceptMethodBody = new InterceptMethodBody(methodFilter);
64            target.WeaveWith(interceptMethodBody);
65        }
66                
67        /// <summary>
68        ///     Intercepts all method bodies on the target item.
69        /// </summary>
70        /// <param name="target">The target to be modified.</param>
71        /// <param name="methodFilter">The method filter that will determine the methods that will be modified.</param>
72        public static void InterceptMethodBody(this TypeDefinition target,
73            Func<MethodReference, bool> methodFilter)
74        {
75            var typeFilter = GetTypeFilter();
76            target.Accept(new ImplementModifiableType(typeFilter));
77
78            IMethodWeaver interceptMethodBody = new InterceptMethodBody(methodFilter);
79            target.WeaveWith(interceptMethodBody);
80        }
81
82        private static Func<TypeReference, bool> GetTypeFilter()
83        {
84            return type =>
85            {
86                var actualType = type.Resolve();
87                if (actualType.IsValueType || actualType.IsInterface)
88                    return false;
89
90                return actualType.IsClass;
91            };
92        }
93    }
94}