PageRenderTime 161ms CodeModel.GetById 70ms app.highlight 3ms RepoModel.GetById 67ms app.codeStats 0ms

/src/LinFu.AOP/ImplementModifiableType.cs

http://github.com/philiplaureano/LinFu
C# | 52 lines | 38 code | 11 blank | 3 comment | 2 complexity | 23ef133a9aaa80c062b16bcb8ac31c8f MD5 | raw file
 1using System;
 2using LinFu.AOP.Interfaces;
 3using LinFu.Reflection.Emit;
 4using Mono.Cecil;
 5
 6namespace LinFu.AOP.Cecil
 7{
 8    internal class ImplementModifiableType : ImplementMethodReplacementHost
 9    {
10        private TypeReference _modifiableInterfaceType;
11
12        public ImplementModifiableType(Func<TypeReference, bool> filter)
13            : base(filter)
14        {
15        }
16
17        public override void ImportReferences(ModuleDefinition module)
18        {
19            // Import the references from the base class
20            base.ImportReferences(module);
21
22            _modifiableInterfaceType = module.ImportType<IModifiableType>();
23        }
24
25        public override bool ShouldWeave(TypeDefinition item)
26        {
27            var shouldWeave = base.ShouldWeave(item);
28
29            // Make sure that the IModifiableType interface is only implemented once
30            shouldWeave &= !item.Interfaces.Contains(_modifiableInterfaceType);
31
32            var isStaticClass = item.IsAbstract && item.IsSealed;
33            shouldWeave &= !isStaticClass;
34
35            return shouldWeave;
36        }
37
38        public override void Weave(TypeDefinition item)
39        {
40            base.Weave(item);
41
42            // Implement IModifiableType
43            if (item.Interfaces.Contains(_modifiableInterfaceType))
44                return;
45
46            item.Interfaces.Add(_modifiableInterfaceType);
47            item.AddProperty("IsInterceptionDisabled", typeof(bool));
48            item.AddProperty("AroundMethodCallProvider", typeof(IAroundInvokeProvider));
49            item.AddProperty("AroundMethodBodyProvider", typeof(IAroundInvokeProvider));
50        }
51    }
52}