C#ASP.NET汎用拡張関数のLogicSugarは簡単で使いやすい

8932 ワード


性能面では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; }
        }

    }


}