using System; using System.Collections.Concurrent; using System.Linq.Expressions; using System.Threading; namespace WCS.Core { public static class Ltc { private static ConcurrentDictionary Channels = new ConcurrentDictionary(); public static void SetChannel(string channel) { Channels[Thread.CurrentThread] = channel; } public static string GetChannel() { return Channels[Thread.CurrentThread]; } public static void Log(string msg) { var channel = GetChannel(); if (channel == "刷新") { return; } DebugPublisher.Publish(GetChannel(), msg); } private static string ResultString(T obj) { if (obj == null) { return "null"; } else if (obj is bool) { var b = obj as Boolean?; return b.Value ? "成立" : "不成立"; } else if (obj is System.Collections.ICollection) { var coll = obj as System.Collections.ICollection; return coll.Count.ToString() + "元素"; } return obj.ToString(); } public static T Do(Expression> exp) { var msg = exp.ExpToString(); msg += " 结果:"; try { var res = exp.Compile().Invoke(); return res; } catch (Exception ex) { msg += ex.GetBaseException().Message; throw; } finally { Log(msg); } } public static T Do(T1 obj, Expression> exp) { var msg = ""; try { try { msg = exp.ExpToString(); } catch (Exception ex2) { } msg += " 结果:"; var res = exp.Compile().Invoke(obj); msg += ResultString(res); return res; } catch (Exception ex) { msg += ex.GetBaseException().Message; throw; } finally { Log(msg); } } public static T Do(T1 obj, T2 obj2, Expression> exp) { var msg = exp.ExpToString(); msg += " 结果:"; try { var res = exp.Compile().Invoke(obj, obj2); msg += ResultString(res); return res; } catch (Exception ex) { msg += ex.GetBaseException().Message; throw; } finally { Log(msg); } } public static T Do(T1 obj, T2 obj2, T3 obj3, Expression> exp) { var msg = exp.ExpToString(); msg += " 结果:"; try { var res = exp.Compile().Invoke(obj, obj2, obj3); msg += ResultString(res); return res; } catch (Exception ex) { msg += ex.GetBaseException().Message; throw; } finally { Log(msg); } } } }