C#、ASP.NET通用扩展工具类之LogicSugar
作者:junjie 时间:2023-11-18 09:56:07
说明一下性能方面 还可以接受 循环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; }
}
}
}
标签:C#,ASP.NET,通用,扩展,工具类,LogicSugar
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Android实现扫一扫识别数字功能
2021-09-27 09:30:57
![](https://img.aspxhome.com/file/2023/3/139563_0s.jpg)
Android编程开发之RadioGroup用法实例
2022-11-11 10:26:27
![](https://img.aspxhome.com/file/2023/2/139062_0s.png)
Java多态的全面系统解析
2023-06-22 13:39:22
![](https://img.aspxhome.com/file/2023/9/77119_0s.png)
Android 显示刷新频率的实现代码
2023-09-18 07:01:34
![](https://img.aspxhome.com/file/2023/5/137555_0s.jpg)
android底部弹出iOS7风格对话选项框(QQ对话框)--第三方开源之IOS_Dialog_Library
2023-02-18 17:42:44
![](https://img.aspxhome.com/file/2023/5/138975_0s.jpg)
Java人民币小写转大写字符串的实现
2021-10-24 00:52:34
SpringBoot yml配置文件读取方法详解
2022-12-13 18:04:19
![](https://img.aspxhome.com/file/2023/6/99916_0s.png)
Spring Security实现微信公众号网页授权功能
2021-07-09 08:28:46
![](https://img.aspxhome.com/file/2023/4/72284_0s.png)
springboot 如何配置多个jndi数据源
2023-03-13 16:28:07
![](https://img.aspxhome.com/file/2023/1/110811_0s.png)
Java线程并发中常见的锁机制详细介绍
2023-07-04 05:33:33
![](https://img.aspxhome.com/file/2023/9/70339_0s.png)
解析C#设计模式之单例模式
2021-12-15 17:41:12
如何基于java语言实现八皇后问题
2022-08-22 09:05:25
SpringBoot利用限速器RateLimiter实现单机限流的示例代码
2023-04-05 19:57:50
使用Deflate算法对文件进行压缩与解压缩的方法详解
2022-01-27 09:48:49
C语言 超详细总结讲解二叉树的概念与使用
2023-11-08 20:24:56
![](https://img.aspxhome.com/file/2023/2/120782_0s.webp)
VS.net VSS时,编译报错:未能向文件“.csproj.FileListAbsolute.txt”写入命令行 对路径 的访问被拒绝。
2021-10-01 18:29:12
C#算法之回文数
2022-06-26 20:02:38
Java Arrays.sort和Collections.sort排序实现原理解析
2022-11-01 01:53:57
![](https://img.aspxhome.com/file/2023/8/79298_0s.jpg)
浅谈Android Studio 4.1 更新内容
2021-09-17 11:27:30
![](https://img.aspxhome.com/file/2023/5/138735_0s.gif)
QT5实现简单的TCP通信的实现
2023-11-02 21:24:48
![](https://img.aspxhome.com/file/2023/1/105171_0s.png)