Last active
July 31, 2017 05:21
-
-
Save sakapon/7235fb5310caa0437efe1d8c12572c40 to your computer and use it in GitHub Desktop.
ProxySample/ProxyableConsole/IProxyable.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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