C#、ASP.NET通用扩展工具类之LogicSugar

所属分类: 软件编程 / C#教程 阅读数: 47
收藏 0 赞 0 分享

说明一下性能方面 还可以接受 循环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; }
    }
 
  }
 
 
}

更多精彩内容其他人还在看

winform用datagridview制作课程表实例

这篇文章主要介绍了winform用datagridview制作课程表的方法,实例分析了WinForm实现课程表的结构、数据库及调用技巧,需要的朋友可以参考下
收藏 0 赞 0 分享

C#中winform控制textbox输入只能为数字的方法

这篇文章主要介绍了C#中winform控制textbox输入只能为数字的方法,包括使用keyPress事件限制键盘输入以及TextChanged事件限制粘贴等情况,来实现控制输入为数字的功能,需要的朋友可以参考下
收藏 0 赞 0 分享

C#省份城市下拉框联动简单实现方法

这篇文章主要介绍了C#省份城市下拉框联动简单实现方法,涉及字典的定义与索引的用法,是非常实用的技巧,需要的朋友可以参考下
收藏 0 赞 0 分享

C#处理MySql多个返回集的方法

这篇文章主要介绍了C#处理MySql多个返回集的方法,实现了对处理MySql多个返回集进行封装,是非常实用的技巧,需要的朋友可以参考下
收藏 0 赞 0 分享

C#无限参数的写法

这篇文章主要介绍了C#无限参数的写法,通过循环遍历再结合paras.Add方法实现无限参数的功能,是比较实用的技巧,需要的朋友可以参考下
收藏 0 赞 0 分享

C#反射应用实例

这篇文章主要介绍了C#反射应用,实例分析了通过反射实现多系统数据库的配置方法,是比较实用的技巧,需要的朋友可以参考下
收藏 0 赞 0 分享

C#窗体传值实例汇总

这篇文章主要介绍了C#窗体传值,实例形式汇总了静态变量传值、委托传值、对话框之间的传值等常见应用技巧,需要的朋友可以参考下
收藏 0 赞 0 分享

C#把数组中的某个元素取出来放到第一个位置的实现方法

这篇文章主要介绍了C#把数组中的某个元素取出来放到第一个位置的实现方法,涉及C#针对数组的常见操作技巧,非常具有实用价值,需要的朋友可以参考下
收藏 0 赞 0 分享

C#中Equality和Identity浅析

这篇文章主要介绍了C#中Equality和Identity浅析,本文先是讲解了Equality和Identity的定义,同时讲解了判断两个对象等价性的4种方法,需要的朋友可以参考下
收藏 0 赞 0 分享

在Linux上运行C#的方法

这篇文章主要介绍了在Linux上运行C#的方法,实例分析了Linux平台下Mono软件包的应用技巧,以及在此基础之上的C#运行方法,具有一定的参考借鉴价值,需要的朋友可以参考下
收藏 0 赞 0 分享
查看更多