说明一下性能方面 还可以接受 循环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 简单好用的更多相关文章

  1. C#ASP.NET 通用扩展函数之 IsWhat 简单好用

    好东西都需要人去整理.分类 注意:需要引用命名空间 SyntacticSugar 用法: /***扩展函数名细***/ //[IsInRange] int num = 100; //以前写法 if ( ...

  2. ASP.NETC#通用扩展函数之TypeParse 类型转换方便多了

    用法: var int1 = "2".TryToInt();//转换为int失败返回0 var int2 = "2x".TryToInt(); var int3 ...

  3. ASP.NET通用权限组件思路设计

    开篇 做任何系统都离不开和绕不过权限的控制,尤其是B/S系统工作原理的特殊性使得权限控制起来更为繁琐,所以就在想是否可以利用IIS的工作原理,在IIS处理客户端请求的某个入口或出口通过判断URL来达到 ...

  4. 程序猿修仙之路--数据结构之你是否真的懂数组? c#socket TCP同步网络通信 用lambda表达式树替代反射 ASP.NET MVC如何做一个简单的非法登录拦截

    程序猿修仙之路--数据结构之你是否真的懂数组?   数据结构 但凡IT江湖侠士,算法与数据结构为必修之课.早有前辈已经明确指出:程序=算法+数据结构  .要想在之后的江湖历练中通关,数据结构必不可少. ...

  5. asp.net mvc 中 一种简单的 URL 重写

    asp.net mvc 中 一种简单的 URL 重写 Intro 在项目中想增加一个公告的功能,但是又不想直接用默认带的那种路由,感觉好low逼,想弄成那种伪静态化的路由 (别问我为什么不直接静态化, ...

  6. ASP.net中导出Excel的简单方法介绍

    下面介绍一种ASP.net中导出Excel的简单方法 先上代码:前台代码如下(这是自己项目里面写的一点代码先贴出来吧) <div id="export" runat=&quo ...

  7. ASP.NET中登录功能的简单逻辑设计

     ASP.NET中登录功能的简单逻辑设计                               概述                               逻辑设计             ...

  8. ASP.NET通用权限系统快速开发框架

    系统在线演示地址: http://120.90.2.126:8051 登录账户:system,密码:system### DEMO下载地址: http://download.csdn.net/detai ...

  9. 通过Knockout.js + ASP.NET Web API构建一个简单的CRUD应用

    REFERENCE FROM : http://www.cnblogs.com/artech/archive/2012/07/04/Knockout-web-api.html 较之面向最终消费者的网站 ...

随机推荐

  1. DataGridView隔行显示不同的颜色

      如果该dataGridView是跟数据库绑定的,则可以触发DataBindingComplete事件:  1private   void   dataGridView1_DataBindingCo ...

  2. IOS6屏幕旋转详解(自动旋转、手动旋转、兼容IOS6之前系统)

    转自 http://blog.csdn.net/zzfsuiye/article/details/8251060 概述: 在iOS6之前的版本中,通常使用 shouldAutorotateToInte ...

  3. 2015想做O2O?那就来看看O2O报告!

    来源:互联网

  4. React直出实现与原理

    前一篇文章我们介绍了虚拟DOM的实现与原理,这篇文章我们来讲讲React的直出. 比起MVVM,React比较容易实现直出,那么React的直出是如何实现,有什么值得我们学习的呢? 为什么MVVM不能 ...

  5. Java 反射练习

    已同步更新至个人blog:http://dxjia.cn/2015/08/java-reflect/ 引用baidubaike上对JAVA反射的说明,如下:JAVA反射机制是在运行状态中,对于任意一个 ...

  6. 【C#】取得并改变图像解析度

    , , bmpOrg.Width, bmpOrg.Height);       g.Dispose();       // 画像を保存       string dirName = Path.GetD ...

  7. 小数量宽带用户的福音,Panabit 云计费easyradius 接口隆重发布,PA宽带计费系统

    PA接口在早前就发布了,但是一直迟迟没有发布官方说明文档,由于最近问的客户较多,特写了这篇文档 由于PA使用标准radius认证协议,所以用户需要在本地搭建一个计费,由于大部分用户的数量只有几百个,不 ...

  8. android Studio NDK

    官方文档地址: https://developer.android.com/studio/projects/add-native-code.html#download-ndk 最近推出CMake方式集 ...

  9. [AX2012 R3]关于Named user license report

    Named user license报表是用来统计各种授权类型用户数的,这里来看看报表数据具体是如何来的.这是一个SSRS的报表,最主要的数据源是来自于类SysUserLicenseCountRepo ...

  10. Reflector反编译.NET文件后修复【转】

    反编译后的工程文件用VS2010打开后,在打开窗体时会出现一系列错误提示: 第一种情况: “设计器无法处理第 152 行的代码: base.AutoScaleMode = AutoScaleMode. ...