C#ASP.NET 通用扩展函数之 LogicSugar 简单好用

 

说明一下性能方面 还可以接受 循环1000次普通Switch是用了0.001秒 ,扩展函数为0.002秒  , 如果是大项目在有负载均衡的情况下完全可以无视掉,小项目也不会计较这点性能了。

 注意需要引用 “SyntacticSugar”

用法:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
//【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);

  

 

 

源码:

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
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; }
        }
 
    }
 
 
}

  

posted @   阿妮亚  阅读(1224)  评论(3编辑  收藏  举报
编辑推荐:
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· 三行代码完成国际化适配,妙~啊~
· .NET Core 中如何实现缓存的预热?
点击右上角即可分享
微信分享提示