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
/// ** 使用说明:http://www.cnblogs.com/sunkaixuan/p/4545338.html
/// </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 简单好用的更多相关文章
- C#ASP.NET 通用扩展函数之 IsWhat 简单好用
好东西都需要人去整理.分类 注意:需要引用命名空间 SyntacticSugar 用法: /***扩展函数名细***/ //[IsInRange] int num = 100; //以前写法 if ( ...
- ASP.NETC#通用扩展函数之TypeParse 类型转换方便多了
用法: var int1 = "2".TryToInt();//转换为int失败返回0 var int2 = "2x".TryToInt(); var int3 ...
- ASP.NET通用权限组件思路设计
开篇 做任何系统都离不开和绕不过权限的控制,尤其是B/S系统工作原理的特殊性使得权限控制起来更为繁琐,所以就在想是否可以利用IIS的工作原理,在IIS处理客户端请求的某个入口或出口通过判断URL来达到 ...
- 程序猿修仙之路--数据结构之你是否真的懂数组? c#socket TCP同步网络通信 用lambda表达式树替代反射 ASP.NET MVC如何做一个简单的非法登录拦截
程序猿修仙之路--数据结构之你是否真的懂数组? 数据结构 但凡IT江湖侠士,算法与数据结构为必修之课.早有前辈已经明确指出:程序=算法+数据结构 .要想在之后的江湖历练中通关,数据结构必不可少. ...
- asp.net mvc 中 一种简单的 URL 重写
asp.net mvc 中 一种简单的 URL 重写 Intro 在项目中想增加一个公告的功能,但是又不想直接用默认带的那种路由,感觉好low逼,想弄成那种伪静态化的路由 (别问我为什么不直接静态化, ...
- ASP.net中导出Excel的简单方法介绍
下面介绍一种ASP.net中导出Excel的简单方法 先上代码:前台代码如下(这是自己项目里面写的一点代码先贴出来吧) <div id="export" runat=&quo ...
- ASP.NET中登录功能的简单逻辑设计
ASP.NET中登录功能的简单逻辑设计 概述 逻辑设计 ...
- ASP.NET通用权限系统快速开发框架
系统在线演示地址: http://120.90.2.126:8051 登录账户:system,密码:system### DEMO下载地址: http://download.csdn.net/detai ...
- 通过Knockout.js + ASP.NET Web API构建一个简单的CRUD应用
REFERENCE FROM : http://www.cnblogs.com/artech/archive/2012/07/04/Knockout-web-api.html 较之面向最终消费者的网站 ...
随机推荐
- GTD时间管理(3)---时间特区图
最近在网上看到一副时间特区图,想象非常深,特点想分享给大家. 从上图可以看出 说明一个人全天的状况 说明: 全天时段 状态 7:30-8:00 是处于起床,穿衣,刷牙,吃早餐 8:00-9:00 是处 ...
- 转载:Cellebrite发布新版手机取证软件,增强调查能力
2012-5-24 7:57:51 文章来源:文传商讯 作者:文传商讯 UFED 1.1.9.7版本为移动取证数据提取.编码和分析提供了先进的技术突破 新闻事实: Cellebrite发布其旗舰产 ...
- IOS开发之不同版本适配问题2(#ifdef __IPHONE_7_0)
继续说说ios不同版本之间的适配 先说一个东西:在xcode当中有一个东西叫targets,苹果的官方文档是这样说的: A target specifies a product to build an ...
- 最新的JavaScript核心语言标准——ES6,彻底改变你编写JS代码的方式!【转载+整理】
原文地址 本文内容 ECMAScript 发生了什么变化? 新标准 版本号6 兑现承诺 迭代器和for-of循环 生成器 Generators 模板字符串 不定参数和默认参数 解构 Destructu ...
- ibatis返回map列表
ibatis返回map列表 1. resultClass="java.util.HashMap" <select id="queryCustmerCarNoBy ...
- 【转】开放api接口签名验证
不要急,源代码分享在最底部,先问大家一个问题,你在写开放的API接口时是如何保证数据的安全性的?先来看看有哪些安全性问题在开放的api接口中,我们通过http Post或者Get方式请求服务器的时候, ...
- Delphi中设置条件断点
写了这么长时间的代码,一直认为调试程序比写程序要重要,上次有人问俺,如何调试一个循环中某个循环条件位置下断点.本来想来在Delphi的断点设置中应该是有一个类似条件断点的东西的,不过我也一直不知道怎么 ...
- C# 字典排序Array.Sort
Array.Sort可以实现便捷的字典排序,但如果完全相信他,那么就容易产生些异常!太顺利了,往往是前面有坑等你. 比如:微信接口,好多地方需要签名认证,签名的时候需要用的字典排序,如果只用Array ...
- Why restTemplate.put() throws “HttpClientErrorException: 404 Not Found”
I make a put request RestTemplate restTemplate = new RestTemplate(); restTemplate.put(new URI(&quo ...
- Java Web dev搭建经验总结
摘要: 原创出处:www.bysocket.com 泥瓦匠BYSocket 希望转载,保留摘要,谢谢! 回馈分析法使我看到,我对专业技术人员,不管是工程师.会计师还是市场研究人员,都容易从直觉上去理解 ...