最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

 更新時間:2015年06月08日 09:57:32   投稿:junjie  
這篇文章主要介紹了C#、ASP.NET通用擴展工具類之LogicSugar,本文直接給出實現(xiàn)代碼和使用方法示例,需要的朋友可以參考下

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

 注意需要引用 “SyntacticSugar”

用法:

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

源碼:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
 
namespace SyntacticSugar
{
  /// <summary>
  /// ** 描述:邏輯糖來簡化你的代碼
  /// ** 創(chuàng)始時間:2015-6-1
  /// ** 修改時間:-
  /// ** 作者:sunkaixuan
  /// </summary>
  public static class LogicSugarExtenions
  {
    /// <summary>
    /// 根據(jù)表達(dá)式的值,來返回兩部分中的其中一個。
    /// </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>
    /// 根據(jù)表達(dá)式的值,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>
    /// 根據(jù)表達(dá)式的值,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>
    /// 根據(jù)表達(dá)式的值,來返回兩部分中的其中一個。
    /// </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;//清空對象,節(jié)約性能
      return reval;
    }
 
    public class SwitchSugarModel<T>
    {
      public T SourceValue { get; set; }
      public bool IsEquals { get; set; }
      public dynamic ReturnVal { get; set; }
    }
 
  }
 
 
}

相關(guān)文章

  • C#實現(xiàn)TFTP客戶端的項目實踐

    C#實現(xiàn)TFTP客戶端的項目實踐

    TFTP不僅有斷點續(xù)傳,多用戶級別限制等功能,本文主要介紹了C#實現(xiàn)TFTP客戶端的項目實踐,具有一定的參考價值,感興趣的可以了解一下
    2024-04-04
  • Unity實現(xiàn)首字母檢索器

    Unity實現(xiàn)首字母檢索器

    這篇文章主要為大家詳細(xì)介紹了Unity實現(xiàn)首字母檢索器,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2020-11-11
  • 分享兩種實現(xiàn)Winform程序的多語言支持的多種解決方案

    分享兩種實現(xiàn)Winform程序的多語言支持的多種解決方案

    本篇文章主要介紹了分享兩種實現(xiàn)Winform程序的多語言支持的多種解決方案,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧。
    2017-02-02
  • C#驗證控件validator的簡單使用

    C#驗證控件validator的簡單使用

    這篇文章主要介紹了C#驗證控件validator的簡單使用方法和示例,十分的簡單實用,有需要的小伙伴可以參考下。
    2015-06-06
  • C#自定義基于控制臺的Timer實例

    C#自定義基于控制臺的Timer實例

    這篇文章主要介紹了C#自定義基于控制臺的Timer實現(xiàn)方法,可以簡單模擬timer控件的相關(guān)功能,具有一定參考借鑒價值,需要的朋友可以參考下
    2015-08-08
  • c#唯一值渲染實例代碼

    c#唯一值渲染實例代碼

    這篇文章主要介紹了c#唯一值渲染實例代碼,有需要的朋友可以參考一下
    2013-12-12
  • C#使用Unity實現(xiàn)剪刀石頭布游戲

    C#使用Unity實現(xiàn)剪刀石頭布游戲

    這篇文章主要為大家詳細(xì)介紹了C#語言使用Unity實現(xiàn)剪刀石頭布游戲,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2019-01-01
  • c#中WebService的介紹及調(diào)用方式小結(jié)

    c#中WebService的介紹及調(diào)用方式小結(jié)

    這篇文章主要給大家介紹了關(guān)于c#中的WebService及其調(diào)用方式的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2018-11-11
  • C#實現(xiàn)在listview中插入圖片實例代碼

    C#實現(xiàn)在listview中插入圖片實例代碼

    這篇文章主要介紹了C#實現(xiàn)在listview中插入圖片實例代碼的相關(guān)資料,需要的朋友可以參考下
    2017-03-03
  • c#使用正則表達(dá)式匹配字符串驗證URL示例

    c#使用正則表達(dá)式匹配字符串驗證URL示例

    這篇文章主要介紹了c#使用正則表達(dá)式的小示例,匹配字符串、驗證URL,大家參考使用吧
    2013-12-12

最新評論

富源县| 南通市| 朔州市| 巩留县| 宜川县| 横山县| 广安市| 内乡县| 南华县| 陵川县| 武隆县| 江门市| 中西区| 砚山县| 丰台区| 长白| 启东市| 虹口区| 高雄市| 个旧市| 吴旗县| 扎鲁特旗| 南雄市| 上杭县| 东台市| 寿宁县| 西吉县| 漳州市| 阜阳市| 钦州市| 石门县| 灌南县| 永济市| 凌源市| 贵溪市| 黑山县| 手游| 高尔夫| 江都市| 滨海县| 北安市|