PageRenderTime 107ms CodeModel.GetById 60ms app.highlight 15ms RepoModel.GetById 26ms app.codeStats 0ms

/src/LinFu.AOP/Extensions/CecilVisitorExtensions.cs

http://github.com/philiplaureano/LinFu
C# | 86 lines | 43 code | 6 blank | 37 comment | 0 complexity | 41ec1df9df1331f23d29f1e24d6d0572 MD5 | raw file
 1using System.Linq;
 2using LinFu.AOP.Cecil.Interfaces;
 3using LinFu.AOP.Interfaces;
 4using Mono.Cecil;
 5
 6namespace LinFu.AOP.Cecil.Extensions
 7{
 8    /// <summary>
 9    ///     A helper class that extends Cecil to support LinFu's weaver model.
10    /// </summary>
11    public static class CecilVisitorExtensions
12    {
13        /// <summary>
14        /// Applies the Type transformation to the target type.
15        /// </summary>
16        /// <param name="host">The target type</param>
17        /// <param name="typeWeaver">The type weaver that will make the current set of modifications.</param>
18        public static void Accept(this TypeDefinition host, ITypeWeaver typeWeaver)
19        {
20            typeWeaver.Weave(host);
21        }
22        
23        /// <summary>
24        /// Applies the Type transformation to the target type.
25        /// </summary>
26        /// <param name="host">The target type</param>
27        /// <param name="typeWeaver">The type weaver that will make the current set of modifications.</param>
28        public static void Accept(this AssemblyDefinition host, ITypeWeaver typeWeaver)
29        {
30            var module = host.MainModule;
31            var types = module.Types.Where(typeWeaver.ShouldWeave).ToArray();
32            foreach (var type in types)
33            {
34                typeWeaver.Weave(type);
35            }
36        }
37        
38        /// <summary>
39        ///     Allows a <see cref="ITypeWeaver" /> instance to traverse any <see cref="IReflectionVisitable" />
40        ///     instance.
41        /// </summary>
42        /// <param name="visitable">The visitable object.</param>
43        /// <param name="typeWeaver">The type weaver.</param>
44        public static void Accept(this IReflectionVisitable visitable, ITypeWeaver typeWeaver)
45        {
46            var visitor = new TypeWeaverVisitor(typeWeaver);
47            visitable.Accept(visitor);
48        }
49
50        /// <summary>
51        ///     Allows a <see cref="ITypeWeaver" /> instance to traverse any <see cref="IReflectionStructureVisitable" />
52        ///     instance.
53        /// </summary>
54        /// <param name="visitable">The visitable object.</param>
55        /// <param name="typeWeaver">The type weaver.</param>
56        public static void Accept(this IReflectionStructureVisitable visitable, ITypeWeaver typeWeaver)
57        {
58            var visitor = new TypeWeaverVisitor(typeWeaver);
59            visitable.Accept(visitor);
60        }
61
62        /// <summary>
63        ///     Allows a <see cref="IMethodWeaver" /> instance to traverse any <see cref="IReflectionVisitable" />
64        ///     instance.
65        /// </summary>
66        /// <param name="visitable">The visitable object.</param>
67        /// <param name="methodWeaver">The method weaver.</param>
68        public static void Accept(this IReflectionStructureVisitable visitable, IMethodWeaver methodWeaver)
69        {
70            var visitor = new MethodWeaverVisitor(methodWeaver);
71            visitable.Accept(visitor);
72        }
73
74        /// <summary>
75        ///     Allows a <see cref="IMethodWeaver" /> instance to traverse any <see cref="IReflectionVisitable" />
76        ///     instance.
77        /// </summary>
78        /// <param name="visitable">The visitable object.</param>
79        /// <param name="methodWeaver">The method weaver.</param>
80        public static void Accept(this IReflectionVisitable visitable, IMethodWeaver methodWeaver)
81        {
82            var visitor = new MethodWeaverVisitor(methodWeaver);
83            visitable.Accept(visitor);
84        }
85    }
86}