Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // added the TActor
- public class Switch<TKey,TDecision,TActor,TParameter> {
- protected static Func<TDecision, bool> Decision;
- protected static Action<TActor, TParameter> OnTrue;
- protected static Action<TActor, TParameter> OnFalse;
- public void Process(TDecision d,TActor o, Func<TParameter> param)
- {
- if (Decision(d))
- {
- OnTrue(o, param());
- }
- else
- {
- OnFalse(o, param());
- }
- }
- public void ProcessOnlyTrue(TDecision d, TActor o, Func<TParameter> param)
- {
- if (Decision(d))
- {
- OnTrue(o, param());
- }
- }
- public void ProcessOnlyFalse(TDecision d,TActor o, Func<TParameter> param)
- {
- if (!Decision(d))
- {
- OnFalse(o, param());
- }
- }
- }
- public class Switch<TKey, TDecision, TParameter> : Switch<TKey,TDecision,TDecision,TParameter> {
- public void Process(TDecision d,Func<TParameter> param ) {
- Process(d,d,param);
- }
- public void ProcessOnlyTrue(TDecision d, Func<TParameter> param)
- {
- ProcessOnlyTrue(d, d, param);
- }
- public void ProcessOnlyFalse(TDecision d, Func<TParameter> param)
- {
- ProcessOnlyFalse(d, d, param);
- }
- }
- public class Debug : Switch<Debug, ILog, string> {
- static Debug() {
- Decision = log => log.IsDebugEnabled;
- OnTrue = (log, msg) => log.Debug(msg);
- }
- }
- public class Info : Switch<Info, ILog, string> {
- static Info() {
- Decision = log => log.IsInfoEnabled;
- OnTrue = (log, msg) => log.Info(msg);
- }
- }
- public static class ILogExtensions {
- public static void Log<T>(this ILog log, Func<string> msg ) where T : Switch<T, ILog, string>,new() {
- new T().ProcessOnlyTrue(log,msg);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement