/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
- using System;
- using LinFu.AOP.Cecil.Interfaces;
- using LinFu.AOP.Interfaces;
- using LinFu.Reflection.Emit;
- using Mono.Cecil;
- namespace LinFu.AOP.Cecil
- {
- internal class ImplementActivatorHostWeaver : ITypeWeaver
- {
- private readonly Func<TypeReference, bool> _filter;
- private TypeReference _activatorPropertyType;
- private TypeReference _hostInterfaceType;
- public ImplementActivatorHostWeaver()
- : this(type => true)
- {
- }
- public ImplementActivatorHostWeaver(Func<TypeReference, bool> filter)
- {
- _filter = filter;
- }
- public bool ShouldWeave(TypeDefinition item)
- {
- if (item.Name == "<Module>")
- return false;
- if (!item.IsClass || item.IsInterface)
- return false;
- if (_filter != null)
- return _filter(item);
- return true;
- }
- public void Weave(TypeDefinition type)
- {
- // Implement IActivatorHost only once
- if (type.Interfaces.Contains(_hostInterfaceType))
- return;
- type.AddProperty("Activator", _activatorPropertyType);
- if (!type.Interfaces.Contains(_hostInterfaceType))
- type.Interfaces.Add(_hostInterfaceType);
- }
- public void AddAdditionalMembers(ModuleDefinition module)
- {
- _hostInterfaceType = module.ImportType<IActivatorHost>();
- _activatorPropertyType = module.ImportType<ITypeActivator>();
- }
- public void ImportReferences(ModuleDefinition module)
- {
- }
- }
- }