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

 更新时间:2015年06月08日 09:57:32   投稿:junjie  
这篇文章主要介绍了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
  /// </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#实现TFTP客户端的项目实践

    C#实现TFTP客户端的项目实践

    TFTP不仅有断点续传,多用户级别限制等功能,本文主要介绍了C#实现TFTP客户端的项目实践,具有一定的参考价值,感兴趣的可以了解一下
    2024-04-04
  • Unity实现首字母检索器

    Unity实现首字母检索器

    这篇文章主要为大家详细介绍了Unity实现首字母检索器,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2020-11-11
  • 分享两种实现Winform程序的多语言支持的多种解决方案

    分享两种实现Winform程序的多语言支持的多种解决方案

    本篇文章主要介绍了分享两种实现Winform程序的多语言支持的多种解决方案,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧。
    2017-02-02
  • C#验证控件validator的简单使用

    C#验证控件validator的简单使用

    这篇文章主要介绍了C#验证控件validator的简单使用方法和示例,十分的简单实用,有需要的小伙伴可以参考下。
    2015-06-06
  • C#自定义基于控制台的Timer实例

    C#自定义基于控制台的Timer实例

    这篇文章主要介绍了C#自定义基于控制台的Timer实现方法,可以简单模拟timer控件的相关功能,具有一定参考借鉴价值,需要的朋友可以参考下
    2015-08-08
  • c#唯一值渲染实例代码

    c#唯一值渲染实例代码

    这篇文章主要介绍了c#唯一值渲染实例代码,有需要的朋友可以参考一下
    2013-12-12
  • C#使用Unity实现剪刀石头布游戏

    C#使用Unity实现剪刀石头布游戏

    这篇文章主要为大家详细介绍了C#语言使用Unity实现剪刀石头布游戏,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2019-01-01
  • c#中WebService的介绍及调用方式小结

    c#中WebService的介绍及调用方式小结

    这篇文章主要给大家介绍了关于c#中的WebService及其调用方式的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2018-11-11
  • C#实现在listview中插入图片实例代码

    C#实现在listview中插入图片实例代码

    这篇文章主要介绍了C#实现在listview中插入图片实例代码的相关资料,需要的朋友可以参考下
    2017-03-03
  • c#使用正则表达式匹配字符串验证URL示例

    c#使用正则表达式匹配字符串验证URL示例

    这篇文章主要介绍了c#使用正则表达式的小示例,匹配字符串、验证URL,大家参考使用吧
    2013-12-12

最新评论