using System.Collections;
using System.Collections.Generic;
public static void Main()
var decoratedFoo = AspectDecorator<IFoo>.Create(foo);
Console.WriteLine("\n\nClass:\n");
Console.WriteLine("\n\nDecorated Class:\n");
public void method1(){Console.WriteLine("> call method1");}
public void method2(){Console.WriteLine("> call method2");}
public void method3(){Console.WriteLine("> call method3");}
public class AspectDecorator<T> : DispatchProxy
protected override object Invoke(MethodInfo targetMethod, object[] args)
Console.WriteLine($"Before : {targetMethod.Name}");
var obj = _impl.GetType().GetMethod(targetMethod.Name).Invoke(_impl, args);
Console.WriteLine($"After : {targetMethod.Name}");
public void SetTarget(T target)
public static T Create(T decorated)
object proxy = Create<T, AspectDecorator<T>>();
((AspectDecorator<T>) proxy)._impl=decorated;