PageRenderTime 16ms CodeModel.GetById 10ms app.highlight 4ms RepoModel.GetById 0ms app.codeStats 0ms

/src/LinFu.AOP/Extensions/TypeDefinitionExtensions.cs

http://github.com/philiplaureano/LinFu
C# | 129 lines | 72 code | 12 blank | 45 comment | 9 complexity | 472325ebe5d53883e821fcd12538dc54 MD5 | raw file
  1using System;
  2using System.Linq;
  3using LinFu.AOP.Cecil.Interfaces;
  4using Mono.Cecil;
  5
  6namespace LinFu.AOP.Cecil.Extensions
  7{
  8    /// <summary>
  9    ///     Adds helper methods to the <see cref="TypeDefinition" /> class.
 10    /// </summary>
 11    public static class TypeDefinitionExtensions
 12    {
 13        /// <summary>
 14        ///     Applies a <see cref="IMethodWeaver" /> instance to all methods
 15        ///     within the given <paramref name="targetType" />.
 16        /// </summary>
 17        /// <param name="targetType">The target module.</param>
 18        /// <param name="weaver">The <see cref="ITypeWeaver" /> instance that will modify the methods in the given target type.</param>
 19        public static void WeaveWith(this TypeDefinition targetType, IMethodWeaver weaver)
 20        {
 21            var module = targetType.Module;
 22            var targetMethods = from MethodDefinition method in targetType.Methods
 23                where weaver.ShouldWeave(method)
 24                select method;
 25
 26            // Modify the host module
 27            weaver.ImportReferences(module);
 28
 29            // Add any additional members to the target type
 30            weaver.AddAdditionalMembers(targetType);
 31
 32            foreach (var item in targetMethods)
 33                weaver.Weave(item);
 34        }
 35
 36        /// <summary>
 37        /// Modifies all the methods in an assembly using the given method weaver.
 38        /// </summary>
 39        /// <param name="targetAssembly">The target assembly to be modified.</param>
 40        /// <param name="weaver">The weaver that will perform the modification.</param>
 41        public static void WeaveWith(this AssemblyDefinition targetAssembly, IMethodWeaver weaver)
 42        {
 43            Func<MethodReference, bool> defaultFilter = m => m.IsDefinition;
 44            WeaveWith(targetAssembly, weaver, defaultFilter);
 45        }
 46
 47        /// <summary>
 48        /// Modifies all the types in an assembly using the given type weaver.
 49        /// </summary>
 50        /// <param name="targetAssembly">The target assembly to be modified.</param>
 51        /// <param name="weaver">The weaver that will perform the modification.</param>
 52        public static void WeaveWith(this AssemblyDefinition targetAssembly, ITypeWeaver weaver)
 53        {
 54            WeaveWith(targetAssembly, weaver, _ => true);
 55        }
 56
 57        /// <summary>
 58        /// Modifies the chosen types in an assembly using the given type weaver. 
 59        /// </summary>
 60        /// <param name="targetAssembly">The target assembly to be modified</param>
 61        /// <param name="weaver">The type weaver that will be used to modify the selected types.</param>
 62        /// <param name="typeFilter">The filter that will determine which types should be modified.</param>
 63        public static void WeaveWith(this AssemblyDefinition targetAssembly, ITypeWeaver weaver,
 64            Func<TypeReference, bool> typeFilter)
 65        {
 66            var module = targetAssembly.MainModule;
 67            var types = module.Types.Where(t => typeFilter(t) && t.IsDefinition && weaver.ShouldWeave(t)).ToArray();
 68            foreach (var type in types)
 69            {
 70                weaver.Weave(type);
 71            }
 72        }
 73        
 74        /// <summary>
 75        /// Modifies the chosen types in an assembly using the given method weaver.
 76        /// </summary>
 77        /// <param name="targetAssembly">The target assembly that will be modified.</param>
 78        /// <param name="weaver">The method weaver that will be used to rewrite all the target methods.</param>
 79        /// <param name="typeFilter">The predicate that will determine which types will be modified.</param>
 80        public static void WeaveWith(this AssemblyDefinition targetAssembly, IMethodWeaver weaver,
 81            Func<TypeReference, bool> typeFilter)
 82        {
 83            var module = targetAssembly.MainModule;
 84            var types = module.Types.Where(t => typeFilter(t) && t.IsDefinition).ToArray();
 85
 86            var methods = types.SelectMany(t => t.Methods.Where(m => m.HasBody && weaver.ShouldWeave(m))).ToArray();
 87            foreach (var method in methods)
 88            {
 89                weaver.Weave(method);
 90            }
 91        }
 92
 93        /// <summary>
 94        /// Modifies the chosen types in an assembly using the given method weaver. 
 95        /// </summary>
 96        /// <param name="targetAssembly">The target assembly that will be modified.</param>
 97        /// <param name="weaver">The method weaver that will be used to rewrite all the target methods.</param>
 98        /// <param name="methodFilter">The predicate that will determine which methods will be modified.</param>
 99        public static void WeaveWith(this AssemblyDefinition targetAssembly, IMethodWeaver weaver,
100            Func<MethodReference, bool> methodFilter)
101        {
102            var module = targetAssembly.MainModule;
103            var types = module.Types.Where(t => !t.IsInterface && !t.IsValueType).ToArray();
104
105            var methods = types.SelectMany(t => t.Methods.Where(m => m.HasBody && weaver.ShouldWeave(m) && methodFilter(m))).ToArray();
106            foreach (var method in methods)
107            {
108                weaver.Weave(method);
109            }
110        }
111        
112        /// <summary>
113        /// Modifies the current type using the given method weaver.
114        /// </summary>
115        /// <param name="targetType">The target type to be modified.</param>
116        /// <param name="weaver">The method weaver that will modify the selected methods.</param>
117        /// <param name="methodFilter">The method filter that will determine which methods should be modified.</param>
118        public static void WeaveWith(this TypeDefinition targetType, IMethodWeaver weaver,
119            Func<MethodReference, bool> methodFilter)
120        {
121            var methods = targetType.Methods.Where(m => m.HasBody && weaver.ShouldWeave(m) && methodFilter(m))
122                .ToArray();
123            foreach (var method in methods)
124            {
125                weaver.Weave(method);
126            }
127        }
128    }
129}