C#、ASP.NET通用擴大對象類之LogicSugar。本站提示廣大學習愛好者:(C#、ASP.NET通用擴大對象類之LogicSugar)文章只能為提供參考,不一定能成為您想要的結果。以下是C#、ASP.NET通用擴大對象類之LogicSugar正文
解釋一下機能方面 還可以接收 輪回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; }
}
}
}