亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

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ù)表達式的值,來返回兩部分中的其中一個。
    /// </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ù)表達式的值,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ù)表達式的值,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ù)表達式的值,來返回兩部分中的其中一個。
    /// </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#使用Pipelines實現(xiàn)處理Socket數(shù)據(jù)包

    C#使用Pipelines實現(xiàn)處理Socket數(shù)據(jù)包

    這篇文章主要為大家詳細(xì)介紹了C#如何使用Pipelines實現(xiàn)處理Socket數(shù)據(jù)包,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下
    2023-12-12
  • 用C#編寫ActiveX控件(二)

    用C#編寫ActiveX控件(二)

    用C#編寫ActiveX控件(二)...
    2007-03-03
  • C# 各種導(dǎo)出的方法總結(jié)

    C# 各種導(dǎo)出的方法總結(jié)

    本篇文章主要介紹了C# 各種導(dǎo)出方法的相關(guān)知識,具有很好的參考價值。下面跟著小編一起來看下吧
    2017-05-05
  • C#實現(xiàn)分治算法求解股票問題

    C#實現(xiàn)分治算法求解股票問題

    本文主要介紹了C#實現(xiàn)分治算法求解股票問題,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2022-04-04
  • 一款域名監(jiān)控小工具 Domain(IP)Watcher 實現(xiàn)代碼

    一款域名監(jiān)控小工具 Domain(IP)Watcher 實現(xiàn)代碼

    域名是否正常,網(wǎng)站是否可以正常訪問是很頭痛的問題,怎樣簡單地監(jiān)控域名是否可以正常訪問呢,這里發(fā)布一款域名監(jiān)控小工具:Domain(IP)Watcher
    2011-11-11
  • C#中DataTable實現(xiàn)行列轉(zhuǎn)換的方法

    C#中DataTable實現(xiàn)行列轉(zhuǎn)換的方法

    這篇文章主要介紹了C#中DataTable實現(xiàn)行列轉(zhuǎn)換的方法,實例分析了C#操作DataTable的相關(guān)技巧,非常具有實用價值,需要的朋友可以參考下
    2015-04-04
  • Winform使用DataGridView實現(xiàn)下拉篩選

    Winform使用DataGridView實現(xiàn)下拉篩選

    這篇文章主要為大家詳細(xì)介紹了Winform如何使用原生DataGridView實現(xiàn)下拉篩選功能,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起了解一下
    2023-09-09
  • C#設(shè)置頁面單位和縮放的方法

    C#設(shè)置頁面單位和縮放的方法

    這篇文章主要介紹了C#設(shè)置頁面單位和縮放的方法,涉及C#設(shè)置頁面屬性的相關(guān)技巧,需要的朋友可以參考下
    2015-06-06
  • C#訪問命令行的兩種方法

    C#訪問命令行的兩種方法

    這篇文章主要介紹了C#訪問命令行的兩種方法,實例分析了C#操作命令行的兩種常用技巧,需要的朋友可以參考下
    2015-06-06
  • Unity腳本自動添加頭部注釋的全過程

    Unity腳本自動添加頭部注釋的全過程

    在一些公司需要代碼嚴(yán)格的管理,有時候會需要用到每個腳本的頭部做一些介紹,所以下面這篇文章主要給大家介紹了關(guān)于Unity腳本自動添加頭部注釋的相關(guān)資料,需要的朋友可以參考下
    2022-01-01

最新評論