Skip to content

Instantly share code, notes, and snippets.

@sakapon
Last active July 31, 2017 05:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sakapon/7235fb5310caa0437efe1d8c12572c40 to your computer and use it in GitHub Desktop.
Save sakapon/7235fb5310caa0437efe1d8c12572c40 to your computer and use it in GitHub Desktop.
ProxySample/ProxyableConsole/IProxyable.cs
using System;
namespace ProxyableConsole
{
public interface IProxyable : IProxyable<object>
{
new void Execute();
}
class BodyProxyable : BodyProxyable<object>, IProxyable
{
public BodyProxyable(Action execute) : base(() =>
{
execute?.Invoke();
return null;
})
{
}
void IProxyable.Execute() => Execute();
}
class AspectProxyable : AspectProxyable<object>, IProxyable
{
public AspectProxyable(IProxyable source, Action<Action> execute) : base(source, f =>
{
execute?.Invoke(() => f());
return null;
})
{
}
void IProxyable.Execute() => Execute();
}
public static class Proxyable
{
public static IProxyable<TResult> Body<TResult>(Func<TResult> execute) => new BodyProxyable<TResult>(execute);
public static IProxyable Body(Action execute) => new BodyProxyable(execute);
public static IProxyable<TResult> Aspect<TResult>(this IProxyable<TResult> source, Func<Func<TResult>, TResult> execute) => new AspectProxyable<TResult>(source, execute);
public static IProxyable Aspect(this IProxyable source, Action<Action> execute) => new AspectProxyable(source, execute);
}
}
using System;
namespace ProxyableConsole
{
class Program
{
static void Main(string[] args)
{
Proxyable.Body(() => Console.WriteLine("Body"))
.Aspect(a =>
{
Console.WriteLine("Before");
a();
Console.WriteLine("After");
})
.Execute();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment