欢迎您来到“名字什么都是浮云”的博客空间!

ClassifyHandler 分类处理结构

 
public class ClassifyHandler
        {
            public object vTrue { get; set; }
            public object vFalse { get; set; }

            public Action fnTrue { get; set; }
            public Action fnFalse { get; set; }

            public static object GetMatchResult(bool ss, ClassifyHandler t2)
            {
                if (ss) return t2.vTrue;
                return t2.vFalse;
            }
            public static void ExecuteMatchEvent(bool ss, ClassifyHandler t2)
            {
                if (ss && t2.fnTrue != null) t2.fnTrue();
                t2.fnFalse?.Invoke();
            }
            public static void ExecuteMatchEvents(object val,Dictionary<object,Action> objs)
            {
                foreach (object item in objs)
                {
                    if (item == val && objs[item] != null)
                    {
                        objs[item]();
                    }
                }
            }
        }
GetMatchResult 替换了以下内容:
var res="";
if(ss){
  res = t2.vTrue;
}
else{
  res = t2.vFalse;
}
ExecuteMatchEvent替换了以下内容:
if(ss){
  t2.fnTrue(); //执行A功能
}
else{
   t2.fnFalse();//执行B功能
}
ExecuteMatchEvents替换了以下内容:
switch(num)
{
   case 1:
      ///
   break;  
   case 2:
      ///
   break;  
   case 3:
      ///
   break;  
}
调用方法
 var ch = new ClassifyHandler()
 {
     vTrue = "true",
     vFalse = "false"
 };

 var dic = new Dictionary<object, Action>();
 dic.Add(2, () =>
 {
     var res = ClassifyHandler.GetMatchResult(!isExport, ch);
 });
 dic.Add(3, () =>
 {
     //处理
 });
 
ClassifyHandler.ExecuteMatchEvents(2
, dic);
posted @ 2017-03-17 12:40  名字什么都是浮云  阅读(284)  评论(0编辑  收藏  举报