PageRenderTime 116ms CodeModel.GetById 112ms app.highlight 2ms RepoModel.GetById 1ms app.codeStats 0ms

/src/LinFu.AOP/ImplementMethodReplacementHost.cs

http://github.com/philiplaureano/LinFu
C# | 57 lines | 44 code | 12 blank | 1 comment | 8 complexity | 33bda347afebe788a44dda9cff54de52 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 ImplementMethodReplacementHost : ITypeWeaver
10    {
11        private readonly Func<TypeReference, bool> _filter;
12        private TypeReference _hostType;
13
14        public ImplementMethodReplacementHost(Func<TypeReference, bool> filter)
15        {
16            _filter = filter;
17        }
18
19
20        public virtual bool ShouldWeave(TypeDefinition item)
21        {
22            if (item.Name == "<Module>")
23                return false;
24
25            if (!item.IsClass || item.IsInterface)
26                return false;
27
28            // Implement the host interface once and only once
29            if (item.Interfaces.Contains(_hostType))
30                return false;
31
32            if (_filter != null)
33                return _filter(item);
34
35            return true;
36        }
37
38        public virtual void Weave(TypeDefinition item)
39        {
40            if (item.Interfaces.Contains(_hostType))
41                return;
42
43            item.Interfaces.Add(_hostType);
44            item.AddProperty("MethodBodyReplacementProvider", typeof(IMethodReplacementProvider));
45            item.AddProperty("MethodCallReplacementProvider", typeof(IMethodReplacementProvider));
46        }
47
48        public virtual void AddAdditionalMembers(ModuleDefinition host)
49        {
50        }
51
52        public virtual void ImportReferences(ModuleDefinition module)
53        {
54            _hostType = module.Import(typeof(IMethodReplacementHost));
55        }
56    }
57}