PageRenderTime 102ms CodeModel.GetById 40ms app.highlight 2ms RepoModel.GetById 58ms app.codeStats 0ms

/examples/LinFu.AOP.Samples/LinFu.AOP.MethodInterceptionSample/Program.cs

http://github.com/philiplaureano/LinFu
C# | 49 lines | 34 code | 9 blank | 6 comment | 2 complexity | 3edb64cd550ef9779f8d93a9020b1981 MD5 | raw file
 1using System;
 2using System.Collections.Generic;
 3using System.Linq;
 4using System.Reflection;
 5using System.Text;
 6using System.Threading;
 7using LinFu.AOP.Cecil.Extensions;
 8using LinFu.AOP.Interfaces;
 9using SampleLibrary;
10
11namespace LinFu.AOP.MethodInterceptionSample
12{
13    public class SampleInterceptor : IInterceptor
14    {
15        public object Intercept(IInvocationInfo info)
16        {
17            var methodName = info.TargetMethod.Name;
18            Console.WriteLine("method '{0}' called", methodName);
19
20            // Replace the input parameter with 42
21            var result = info.TargetMethod.Invoke(info.Target, new object[]{42});
22
23            return result;
24        }
25    }
26    
27
28    // NOTE: Remember to check the SampleLibrary.csproj file for an example on how to use LinFu.AOP.Tasks to transparently
29    // postweave your DLLs!
30    class Program
31    {
32        static void Main(string[] args)
33        {
34            var employee = new Employee();
35
36            // All LinFu.AOP-modified objects implement the IModifiableType interface
37            var modifiableType = employee as IModifiableType;
38
39            // Plug in our custom implementation
40            if (modifiableType != null)
41                modifiableType.MethodBodyReplacementProvider = new SimpleMethodReplacementProvider(new SampleInterceptor());
42
43            // The employee object will call the interceptor instead of the actual method implementation
44            employee.Pay(12345);
45
46            return;
47        }
48    }
49}