PageRenderTime 27ms CodeModel.GetById 22ms app.highlight 2ms RepoModel.GetById 2ms app.codeStats 0ms

/src/LinFu.AOP/TypeWeaverVisitor.cs

http://github.com/philiplaureano/LinFu
C# | 50 lines | 31 code | 7 blank | 12 comment | 3 complexity | 2e45ca5392c5c849b65fa8b72619164f MD5 | raw file
 1using System.Collections.Generic;
 2using LinFu.AOP.Cecil.Interfaces;
 3using Mono.Cecil;
 4
 5namespace LinFu.AOP.Cecil
 6{
 7    /// <summary>
 8    ///     Represents a visitor class that can iterate over <see cref="TypeDefinition" />
 9    ///     instances.
10    /// </summary>
11    public class TypeWeaverVisitor : BaseReflectionVisitor
12    {
13        private readonly HashSet<ModuleDefinition> _visitedModules = new HashSet<ModuleDefinition>();
14        private readonly ITypeWeaver _weaver;
15
16        /// <summary>
17        ///     Initializes a new instance of the TypeWeaverVisitor class.
18        /// </summary>
19        /// <param name="weaver">The <see cref="ITypeWeaver" /> that will be used to modify a given type.</param>
20        public TypeWeaverVisitor(ITypeWeaver weaver)
21        {
22            _weaver = weaver;
23        }
24
25        /// <summary>
26        ///     Visits a <see cref="TypeDefinition" /> instance.
27        /// </summary>
28        /// <param name="type">A <see cref="TypeDefinition" /> object.</param>
29        public override void VisitTypeDefinition(TypeDefinition type)
30        {
31            if (type.IsEnum)
32                return;
33
34            if (!_weaver.ShouldWeave(type))
35                return;
36
37            var module = type.Module;
38            if (!_visitedModules.Contains(module))
39            {
40                _weaver.ImportReferences(module);
41                _weaver.AddAdditionalMembers(module);
42                _visitedModules.Add(module);
43            }
44
45            _weaver.Weave(type);
46
47            base.VisitTypeDefinition(type);
48        }
49    }
50}