C#、ASP.NET汎用拡張ツール類のLogicSugar
6853 ワード
性能面では1000サイクルも許容できる普通のSwitchは0.001秒、拡張関数は0.002秒で、大項目であれば負荷均衡がある場合は完全に無視でき、小項目でもこの性能にこだわることはありません.
注意「SyntacticSugar」を参照してください
使用方法:
注意「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
{
///
/// ** :
/// ** :2015-6-1
/// ** :-
/// ** :sunkaixuan
///
public static class LogicSugarExtenions
{
///
/// , 。
///
///
///
/// true trueValue
/// false falseValue
///
public static T IIF(this bool thisValue, T trueValue, T falseValue)
{
if (thisValue)
{
return trueValue;
}
else
{
return falseValue;
}
}
///
/// ,true trueValue,false string.Empty;
///
///
///
/// true trueValue
///
public static string IIF(this bool thisValue, string trueValue)
{
if (thisValue)
{
return trueValue;
}
else
{
return string.Empty;
}
}
///
/// ,true trueValue,false 0
///
///
///
/// true trueValue
///
public static int IIF(this bool thisValue, int trueValue)
{
if (thisValue)
{
return trueValue;
}
else
{
return 0;
}
}
///
/// , 。
///
///
///
/// true trueValue
/// false falseValue
///
public static T IIF(this bool? thisValue, T trueValue, T falseValue)
{
if (thisValue == true)
{
return trueValue;
}
else
{
return falseValue;
}
}
///
/// true, true false
///
///
///
///
public static bool And(this bool thisValue, params bool[] andValues)
{
return thisValue && !andValues.Where(c => c == false).Any();
}
///
/// true, true false
///
///
///
///
public static bool Or(this bool thisValue, params bool[] andValues)
{
return thisValue || andValues.Where(c => c == true).Any();
}
///
/// Switch().Case().Default().Break()
///
///
///
///
public static SwitchSugarModel Switch(this T thisValue)
{
var reval = new SwitchSugarModel();
reval.SourceValue = thisValue;
return reval;
}
public static SwitchSugarModel Case(this SwitchSugarModel switchSugar, T caseValue, TReturn returnValue)
{
if (switchSugar.SourceValue.Equals(caseValue))
{
switchSugar.IsEquals = true;
switchSugar.ReturnVal = returnValue;
}
return switchSugar;
}
public static SwitchSugarModel Default(this SwitchSugarModel switchSugar, TReturn returnValue)
{
if (switchSugar.IsEquals == false)
switchSugar.ReturnVal = returnValue;
return switchSugar;
}
public static dynamic Break(this SwitchSugarModel switchSugar)
{
string reval = switchSugar.ReturnVal;
switchSugar = null;// ,
return reval;
}
public class SwitchSugarModel
{
public T SourceValue { get; set; }
public bool IsEquals { get; set; }
public dynamic ReturnVal { get; set; }
}
}
}