说明一下性能方面 还可以接受 循环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. JS中的各种检测

    //null 只在肯定返回null值时才使用null比较 var element = document.getElementById("my-div"); if (element ...

  2. EWM 强大的数据修复功能

    在上了EWM系统后,运行一段时间可能因为不正确的操作,系统意外情况数据不一致的问题,交货单行项目状态不致,等等报不一致的情况,EWM的自检功能比较强. 下面介绍一种数据不致的修复工具之一,tx: /S ...

  3. Initializing a collection

    Before Java 1.7, only this one is permitted: ArrayList<String> a = new ArrayList<String> ...

  4. “Adobe Flash Player因过期而遭到阻止”的解决办法

    谷歌浏览器总是提示“Adobe Flash Player因过期而遭到阻止”,然后点进去,又更新不了,因为伟大的TC已经把谷歌屏蔽了. 解决办法就是到flash官网更新到最新的Flash https:/ ...

  5. java目录

    1. 在jsp文件或Servlet中,可以通过getServletContext().getRealPath("/")来获取项目根目录的绝对路径. 2. Java桌面程序中,可以通 ...

  6. 解决eclipse使用Search弹出错误问题

    在eclipse中搜索时,搜索完之后有时候会弹出错误对话框,虽然错误内容有时候不同,但是解决办法都一样. 这个问题是由于eclipse中文件不同步引起的.在eclipse中,工程文件是由eclipse ...

  7. 高并发WCF配置

    在使用WCF做服务接口时,TCP模式肯定比Http效率高,Binary/MTOM格式的绑定也会Text格式的绑定高效. 两个endpoint,一个用来调试:ms-mex的binding是用来方便WCF ...

  8. 【LeetCode】257. Binary Tree Paths

    Binary Tree Paths Given a binary tree, return all root-to-leaf paths. For example, given the followi ...

  9. WordPress添加固定位置的百度分享按钮

    第一步,在你所用主题目录新建一个名称为:share.php的模板文件,将下面的代码复制到进去并保存 <div id="share"> <div class=&qu ...

  10. 【规范】javascript 变量命名规则

    javascript 有三大经典的变量命名法:匈牙利命名法,驼峰式命名法和帕斯卡命名法.今天主要介绍下这三种命名方式. 匈牙利命名法 语法 变量名 = 类型 + 对象描述 类型指变量的类型 对象描述指 ...