PageRenderTime 82ms CodeModel.GetById 40ms app.highlight 3ms RepoModel.GetById 37ms app.codeStats 0ms

/src/LinFu.AOP/MethodWeaverVisitor.cs

http://github.com/philiplaureano/LinFu
C# | 65 lines | 34 code | 7 blank | 24 comment | 1 complexity | 66d862484bc302b69b8969f6d7da12bc MD5 | raw file
 1using LinFu.AOP.Cecil.Interfaces;
 2using Mono.Cecil;
 3
 4namespace LinFu.AOP.Cecil
 5{
 6    /// <summary>
 7    ///     Represents a visitor class that can iterate over <see cref="MethodDefinition" />
 8    ///     instances.
 9    /// </summary>
10    public class MethodWeaverVisitor : BaseReflectionVisitor
11    {
12        private readonly IMethodWeaver _methodWeaver;
13
14        /// <summary>
15        ///     Initializes a new instance of the MethodWeaverVisitor class.
16        /// </summary>
17        /// <param name="methodWeaver">The <see cref="IMethodWeaver" /> that will be used to modify a given type.</param>
18        public MethodWeaverVisitor(IMethodWeaver methodWeaver)
19        {
20            _methodWeaver = methodWeaver;
21        }
22
23        /// <summary>
24        ///     Visits a <see cref="MethodDefinition" /> instance.
25        /// </summary>
26        /// <param name="ctor">The <see cref="MethodDefinition" /> instance that will be modified.</param>
27        public override void VisitConstructor(MethodDefinition ctor)
28        {
29            VisitMethodDefinition(ctor);
30        }
31
32        /// <summary>
33        ///     Visits a <see cref="MethodDefinition" /> instance.
34        /// </summary>
35        /// <param name="method">The <see cref="MethodDefinition" /> instance that will be modified.</param>
36        public override void VisitMethodDefinition(MethodDefinition method)
37        {
38            if (!_methodWeaver.ShouldWeave(method))
39                return;
40
41            _methodWeaver.Weave(method);
42            base.VisitMethodDefinition(method);
43        }
44
45        /// <summary>
46        ///     Visits a <see cref="ModuleDefinition" /> instance.
47        /// </summary>
48        /// <param name="module">A <see cref="ModuleDefinition" /> object.</param>
49        public override void VisitModuleDefinition(ModuleDefinition module)
50        {
51            _methodWeaver.ImportReferences(module);
52            base.VisitModuleDefinition(module);
53        }
54
55        /// <summary>
56        ///     Visits a <see cref="TypeDefinition" /> instance.
57        /// </summary>
58        /// <param name="type">A <see cref="TypeDefinition" /> object.</param>
59        public override void VisitTypeDefinition(TypeDefinition type)
60        {
61            _methodWeaver.AddAdditionalMembers(type);
62            base.VisitTypeDefinition(type);
63        }
64    }
65}