当前位置: 移动技术网 > IT编程>开发语言>c# > C#、ASP.NET通用扩展工具类之LogicSugar

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

2019年07月18日  | 移动技术网IT编程  | 我要评论
说明一下性能方面 还可以接受 循环1000次普通switch是用了0.001秒 ,扩展函数为0.002秒  , 如果是大项目在有负载均衡的情况下完全可以无视掉,小

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

如您对本文有疑问或者有任何想说的,请点击进行留言回复,万千网友为您解惑!

相关文章:

验证码:
移动技术网