PageRenderTime 82ms CodeModel.GetById 40ms app.highlight 3ms RepoModel.GetById 38ms app.codeStats 0ms

/src/LinFu.AOP/Factories/AssemblyWeaverActionFactory.cs

http://github.com/philiplaureano/LinFu
C# | 43 lines | 26 code | 3 blank | 14 comment | 0 complexity | 1bd952fa02b04cb1f406d19ab1d2fcf3 MD5 | raw file
 1using System;
 2using LinFu.IoC.Configuration;
 3using LinFu.IoC.Interfaces;
 4using Mono.Cecil;
 5
 6namespace LinFu.AOP.Cecil.Factories
 7{
 8    /// <summary>
 9    ///     Represents a class that generates <see cref="Action{T1,T2}" /> instances
10    ///     that apply a specific method weaver (with the name given in the first delegate parameter)
11    ///     to every type in every module of an <see cref="AssemblyDefinition" /> instance.
12    /// </summary>
13    [Factory(typeof(Action<string, AssemblyDefinition>), ServiceName = "AssemblyWeaver")]
14    public class AssemblyWeaverActionFactory : IFactory
15    {
16        /// <summary>
17        ///     Generates <see cref="Action{T1, T2}" /> instances
18        ///     that apply a specific method weaver (with the name given in the first delegate parameter)
19        ///     to every type in every module of an <see cref="AssemblyDefinition" /> instance.
20        /// </summary>
21        /// <param name="request">The <see cref="IFactoryRequest" /> that describes the service request.</param>
22        /// <returns>An action delegate that will apply a specific method weaver to all the types in the given assembly.</returns>
23        public object CreateInstance(IFactoryRequest request)
24        {
25            var container = request.Container;
26            Action<string, AssemblyDefinition> result =
27                (weaverName, assembly) =>
28                {
29                    // Create the lambda that can modify the target types
30                    var weaveWith =
31                        (Action<string, TypeDefinition>)
32                        container.GetService("TypeWeaver", typeof(Action<string, TypeDefinition>));
33                    var mainModule = assembly.MainModule;
34
35                    foreach (TypeDefinition type in mainModule.Types)
36                        // Use the method weaver on the target type
37                        weaveWith(weaverName, type);
38                };
39
40            return result;
41        }
42    }
43}