PageRenderTime 112ms CodeModel.GetById 60ms app.highlight 4ms RepoModel.GetById 46ms app.codeStats 0ms

/src/LinFu.AOP/FieldInterception/ImplementFieldInterceptionHostWeaver.cs

http://github.com/philiplaureano/LinFu
C# | 81 lines | 44 code | 12 blank | 25 comment | 8 complexity | 99de68d4b0f861cc760bce32ec2117ed MD5 | raw file
 1using System;
 2using LinFu.AOP.Cecil.Interfaces;
 3using LinFu.AOP.Interfaces;
 4using LinFu.Reflection.Emit;
 5using Mono.Cecil;
 6
 7namespace LinFu.AOP.Cecil
 8{
 9    /// <summary>
10    ///     Represents a type weaver that modifies types to implement the <see cref="IFieldInterceptionHost" /> interface.
11    /// </summary>
12    public class ImplementFieldInterceptionHostWeaver : ITypeWeaver
13    {
14        private readonly Func<TypeReference, bool> _filter;
15        private TypeReference _hostInterfaceType;
16        private TypeReference _interceptorPropertyType;
17
18        /// <summary>
19        ///     Initializes a new instance of the ImplementFieldInterceptionHostWeaver class.
20        /// </summary>
21        /// <param name="filter">The filter that determines which types should be modified.</param>
22        public ImplementFieldInterceptionHostWeaver(Func<TypeReference, bool> filter)
23        {
24            _filter = filter;
25        }
26
27
28        /// <summary>
29        ///     Determines whether or not a type should be modified.
30        /// </summary>
31        /// <param name="item"></param>
32        /// <returns><c>true</c> if the type should be modified; otherwise, it will return <c>false</c>.</returns>
33        public bool ShouldWeave(TypeDefinition item)
34        {
35            if (item.Name == "<Module>")
36                return false;
37
38            if (!item.IsClass || item.IsInterface)
39                return false;
40
41            if (_filter != null)
42                return _filter(item);
43
44            return true;
45        }
46
47        /// <summary>
48        ///     Modifies the target type.
49        /// </summary>
50        /// <param name="type">The type to be modified.</param>
51        public void Weave(TypeDefinition type)
52        {
53            // Implement IActivatorHost only once
54            if (type.Interfaces.Contains(_hostInterfaceType))
55                return;
56
57            type.AddProperty("FieldInterceptor", _interceptorPropertyType);
58
59            if (!type.Interfaces.Contains(_hostInterfaceType))
60                type.Interfaces.Add(_hostInterfaceType);
61        }
62
63        /// <summary>
64        ///     Adds additional members to the target module.
65        /// </summary>
66        /// <param name="host">The host module.</param>
67        public void AddAdditionalMembers(ModuleDefinition host)
68        {
69        }
70
71        /// <summary>
72        ///     Imports references into the target module.
73        /// </summary>
74        /// <param name="module">The module containing the type to be modified.</param>
75        public void ImportReferences(ModuleDefinition module)
76        {
77            _hostInterfaceType = module.ImportType<IFieldInterceptionHost>();
78            _interceptorPropertyType = module.ImportType<IFieldInterceptor>();
79        }
80    }
81}