国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > C# > 正文

C#、ASP.NET通用擴展工具類之LogicSugar

2020-01-24 01:46:27
字體:
來源:轉載
供稿:網友

說明一下性能方面 還可以接受 循環1000次普通Switch是用了0.001秒 ,擴展函數為0.002秒  , 如果是大項目在有負載均衡的情況下完全可以無視掉,小項目也不會計較這點性能了。

 注意需要引用 “SyntacticSugar”

用法:

//【Switch】string bookKey = "c#"; //以前寫法string myBook = "";switch (bookKey){  case "c#":    myBook = "asp.net技術";    break;  case "java":    myBook = "java技術";    break;  case "sql":    myBook = "mssql技術";    break;  default:    myBook = "要飯技術";    break;//打這么多break和封號,手痛嗎?} //現在寫法myBook =bookKey.Switch().Case("c#", "asp.net技術").Case("java", "java技術").Case("sql", "sql技術").Default("要飯技術").Break();//點的爽啊    /**   C#類里看不出效果, 在mvc razor里  ? 、&& 或者 || 直接使用都會報錯,需要外面加一個括號,嵌套多了很不美觀,使用自定義擴展函數就沒有這種問題了。  */ bool isSuccess = true; //【IIF】//以前寫法var trueVal1 = isSuccess ? 100 :0;//現在寫法var trueVal2 = isSuccess.IIF(100); //以前寫法var str = isSuccess ? "我是true" : "";//現在寫法var str2 = isSuccess.IIF("我是true");  //以前寫法var trueVal3 = isSuccess ? 1 : 2;//現在寫法var trueVal4 = isSuccess.IIF(1, 2);   string id = "";string id2 = ""; //以前寫法isSuccess = (id == id2) && (id != null && Convert.ToInt32(id) > 0);//現在寫法isSuccess = (id == id2).And(id != null, Convert.ToInt32(id) > 0); //以前寫法isSuccess = id != null || id != id2;//現在寫法isSuccess = (id != null).Or(id != id2);

源碼:

using System;using System.Collections.Generic;using System.Linq;using System.Text; namespace SyntacticSugar{  /// <summary>  /// ** 描述:邏輯糖來簡化你的代碼  /// ** 創始時間:2015-6-1  /// ** 修改時間:-  /// ** 作者:sunkaixuan  /// </summary>  public static class LogicSugarExtenions  {    /// <summary>    /// 根據表達式的值,來返回兩部分中的其中一個。    /// </summary>    /// <typeparam name="T"></typeparam>    /// <param name="thisValue"></param>    /// <param name="trueValue">值為true返回 trueValue</param>    /// <param name="falseValue">值為false返回 falseValue</param>    /// <returns></returns>    public static T IIF<T>(this bool thisValue, T trueValue, T falseValue)    {      if (thisValue)      {        return trueValue;      }      else      {        return falseValue;      }    }      /// <summary>    /// 根據表達式的值,true返回trueValue,false返回string.Empty;    /// </summary>    /// <typeparam name="T"></typeparam>    /// <param name="thisValue"></param>    /// <param name="trueValue">值為true返回 trueValue</param>    /// <returns></returns>    public static string IIF(this bool thisValue, string trueValue)    {      if (thisValue)      {        return trueValue;      }      else      {        return string.Empty;      }    }       /// <summary>    /// 根據表達式的值,true返回trueValue,false返回0    /// </summary>    /// <typeparam name="T"></typeparam>    /// <param name="thisValue"></param>    /// <param name="trueValue">值為true返回 trueValue</param>    /// <returns></returns>    public static int IIF(this bool thisValue, int trueValue)    {      if (thisValue)      {        return trueValue;      }      else      {        return 0;      }    }      /// <summary>    /// 根據表達式的值,來返回兩部分中的其中一個。    /// </summary>    /// <typeparam name="T"></typeparam>    /// <param name="thisValue"></param>    /// <param name="trueValue">值為true返回 trueValue</param>    /// <param name="falseValue">值為false返回 falseValue</param>    /// <returns></returns>    public static T IIF<T>(this bool? thisValue, T trueValue, T falseValue)    {      if (thisValue == true)      {        return trueValue;      }      else      {        return falseValue;      }    }       /// <summary>    /// 所有值為true,則返回true否則返回false    /// </summary>    /// <param name="thisValue"></param>    /// <param name="andValues"></param>    /// <returns></returns>    public static bool And(this bool thisValue, params bool[] andValues)    {      return thisValue && !andValues.Where(c => c == false).Any();    }      /// <summary>    /// 只要有一個值為true,則返回true否則返回false    /// </summary>    /// <param name="thisValue"></param>    /// <param name="andValues"></param>    /// <returns></returns>    public static bool Or(this bool thisValue, params bool[] andValues)    {      return thisValue || andValues.Where(c => c == true).Any();    }      /// <summary>    /// Switch().Case().Default().Break()    /// </summary>    /// <typeparam name="T"></typeparam>    /// <param name="thisValue"></param>    /// <returns></returns>    public static SwitchSugarModel<T> Switch<T>(this T thisValue)    {      var reval = new SwitchSugarModel<T>();      reval.SourceValue = thisValue;      return reval;     }     public static SwitchSugarModel<T> Case<T, TReturn>(this SwitchSugarModel<T> switchSugar, T caseValue, TReturn returnValue)    {      if (switchSugar.SourceValue.Equals(caseValue))      {        switchSugar.IsEquals = true;        switchSugar.ReturnVal = returnValue;      }      return switchSugar;    }     public static SwitchSugarModel<T> Default<T, TReturn>(this SwitchSugarModel<T> switchSugar, TReturn returnValue)    {      if (switchSugar.IsEquals == false)        switchSugar.ReturnVal = returnValue;      return switchSugar;    }     public static dynamic Break<T>(this SwitchSugarModel<T> switchSugar)    {      string reval = switchSugar.ReturnVal;      switchSugar = null;//清空對象,節約性能      return reval;    }     public class SwitchSugarModel<T>    {      public T SourceValue { get; set; }      public bool IsEquals { get; set; }      public dynamic ReturnVal { get; set; }    }   }  }

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 远安县| 霍林郭勒市| 比如县| 明水县| 黄大仙区| 高唐县| 玉林市| 台前县| 鄂尔多斯市| 福海县| 平阳县| 长寿区| 得荣县| 梅河口市| 阿克| 木里| 湄潭县| 通城县| 漳平市| 新巴尔虎右旗| 永靖县| 襄汾县| 平顺县| 平乐县| 延津县| 南川市| 冕宁县| 体育| 灵璧县| 中牟县| 城步| 南宫市| 大同县| 卢氏县| 兴化市| 卢龙县| 神农架林区| 宝坻区| 漳浦县| 洛宁县| 金溪县|