PageRenderTime 123ms CodeModel.GetById 81ms app.highlight 3ms RepoModel.GetById 37ms app.codeStats 0ms

/src/LinFu.AOP/NewOperatorInterception/ImplementActivatorHostWeaver.cs

http://github.com/philiplaureano/LinFu
C# | 62 lines | 48 code | 13 blank | 1 comment | 8 complexity | 73a7b69a621e2f2b63cb3a547b45cffe 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    internal class ImplementActivatorHostWeaver : ITypeWeaver
10    {
11        private readonly Func<TypeReference, bool> _filter;
12        private TypeReference _activatorPropertyType;
13        private TypeReference _hostInterfaceType;
14
15        public ImplementActivatorHostWeaver()
16            : this(type => true)
17        {
18        }
19
20        public ImplementActivatorHostWeaver(Func<TypeReference, bool> filter)
21        {
22            _filter = filter;
23        }
24
25
26        public bool ShouldWeave(TypeDefinition item)
27        {
28            if (item.Name == "<Module>")
29                return false;
30
31            if (!item.IsClass || item.IsInterface)
32                return false;
33
34            if (_filter != null)
35                return _filter(item);
36
37            return true;
38        }
39
40        public void Weave(TypeDefinition type)
41        {
42            // Implement IActivatorHost only once
43            if (type.Interfaces.Contains(_hostInterfaceType))
44                return;
45
46            type.AddProperty("Activator", _activatorPropertyType);
47
48            if (!type.Interfaces.Contains(_hostInterfaceType))
49                type.Interfaces.Add(_hostInterfaceType);
50        }
51
52        public void AddAdditionalMembers(ModuleDefinition module)
53        {
54            _hostInterfaceType = module.ImportType<IActivatorHost>();
55            _activatorPropertyType = module.ImportType<ITypeActivator>();
56        }
57
58        public void ImportReferences(ModuleDefinition module)
59        {
60        }
61    }
62}