移动系统liao
2024-07-30 35a617123c0d578772d9f4450bf27d3b52277384
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
/***********************************************************************
 *            Project: baifenBinfa.Net                                     *
 *                Web: https://baifenBinfa.com                             *
 *        ProjectName: 百分兵法管理系统                               *
 *             Author:                                        *
 *              Email:                               *
 *           Versions: 1.0                                             *
 *         CreateTime: 2020-02-01 17:48:52
 *          NameSpace: CoreCms.Net.Framework.Utility.Extensions
 *           FileName: ConvertExtensions
 *   ClassDescription:
 ***********************************************************************/
 
 
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Text;
 
namespace CoreCms.Net.Utility.Extensions
{
    /// <summary>
    /// 扩展数据转换
    /// </summary>
    public static class ObjectExtensions
    {
        /// <summary>
        /// 数据转换为int类型
        /// </summary>
        /// <param name="thisValue"></param>
        /// <returns></returns>
        public static int ObjectToInt(this object thisValue)
        {
            int result = 0;
            if (thisValue == null)
                return 0;
            return thisValue != null && thisValue != DBNull.Value && int.TryParse(thisValue.ToString(), out result) ? result : result;
        }
 
        /// <summary>
        /// 数据转换为int类型
        /// </summary>
        /// <param name="thisValue"></param>
        /// <param name="errorValue"></param>
        /// <returns></returns>
        public static int ObjectToInt(this object thisValue, int errorValue)
        {
            int result = 0;
            return thisValue != null && thisValue != DBNull.Value && int.TryParse(thisValue.ToString(), out result) ? result : errorValue;
        }
 
        /// <summary>
        /// 数据转换为Double类型
        /// </summary>
        /// <param name="thisValue"></param>
        /// <returns></returns>
        public static double ObjectToDouble(this object thisValue)
        {
            double result = 0.0;
            return thisValue != null && thisValue != DBNull.Value && double.TryParse(thisValue.ToString(), out result) ? result : 0.0;
        }
 
        /// <summary>
        /// 数据转换为Double类型
        /// </summary>
        /// <param name="thisValue"></param>
        /// <param name="errorValue"></param>
        /// <returns></returns>
        public static double ObjectToDouble(this object thisValue, double errorValue)
        {
            double result = 0.0;
            return thisValue != null && thisValue != DBNull.Value && double.TryParse(thisValue.ToString(), out result) ? result : errorValue;
        }
 
        /// <summary>
        /// 数据转换为Float类型
        /// </summary>
        /// <param name="thisValue"></param>
        /// <returns></returns>
        public static float ObjectToFloat(this object thisValue)
        {
            float result = 0;
            return thisValue != null && thisValue != DBNull.Value && float.TryParse(thisValue.ToString(), out result) ? result : 0;
        }
 
        /// <summary>
        /// 数据转换为Float类型
        /// </summary>
        /// <param name="thisValue"></param>
        /// <param name="errorValue"></param>
        /// <returns></returns>
        public static float ObjectToFloat(this object thisValue, float errorValue)
        {
            float result = 0;
            return thisValue != null && thisValue != DBNull.Value && float.TryParse(thisValue.ToString(), out result) ? result : errorValue;
        }
 
        /// <summary>
        /// 数据转换为String类型
        /// </summary>
        /// <param name="thisValue"></param>
        /// <returns></returns>
        public static string ObjectToString(this object thisValue)
        {
            return thisValue != null ? thisValue.ToString().Trim() : "";
        }
 
        /// <summary>
        /// 数据转换为String类型
        /// </summary>
        /// <param name="thisValue"></param>
        /// <param name="errorValue"></param>
        /// <returns></returns>
        public static string ObjectToString(this object thisValue, string errorValue)
        {
            return thisValue != null ? thisValue.ToString().Trim() : errorValue;
        }
 
        /// <summary>
        /// 数据转换为Decimal类型
        /// </summary>
        /// <param name="thisValue"></param>
        /// <returns></returns>
        public static Decimal ObjectToDecimal(this object thisValue)
        {
            Decimal result = new Decimal();
            return thisValue != null && thisValue != DBNull.Value && Decimal.TryParse(thisValue.ToString(), out result) ? result : Decimal.Zero;
        }
 
        /// <summary>
        /// 数据转换为Decimal类型
        /// </summary>
        /// <param name="thisValue"></param>
        /// <param name="errorValue"></param>
        /// <returns></returns>
        public static Decimal ObjectToDecimal(this object thisValue, Decimal errorValue)
        {
            Decimal result = new Decimal();
            return thisValue != null && thisValue != DBNull.Value && Decimal.TryParse(thisValue.ToString(), out result) ? result : errorValue;
        }
 
        /// <summary>
        /// 数据转换为DateTime类型
        /// </summary>
        /// <param name="thisValue"></param>
        /// <returns></returns>
        public static DateTime ObjectToDate(this object thisValue)
        {
            DateTime result = DateTime.MinValue;
            if (thisValue != null && thisValue != DBNull.Value && DateTime.TryParse(thisValue.ToString(), out result))
                result = Convert.ToDateTime(thisValue);
            return result;
        }
 
        /// <summary>
        /// 数据转换为DateTime类型
        /// </summary>
        /// <param name="thisValue"></param>
        /// <param name="errorValue"></param>
        /// <returns></returns>
        public static DateTime ObjectToDate(this object thisValue, DateTime errorValue)
        {
            DateTime result = DateTime.MinValue;
            return thisValue != null && thisValue != DBNull.Value && DateTime.TryParse(thisValue.ToString(), out result) ? result : errorValue;
        }
 
        /// <summary>
        /// 数据转换为bool类型
        /// </summary>
        /// <param name="thisValue"></param>
        /// <returns></returns>
        public static bool ObjectToBool(this object thisValue)
        {
            bool result = false;
            return thisValue != null && thisValue != DBNull.Value && bool.TryParse(thisValue.ToString(), out result) ? result : result;
        }
 
        /// <summary>
        /// 获取枚举的标注的值
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="value"></param>
        /// <returns></returns>
        public static string GetDescription(this Enum value) 
        {
            var field = value.GetType().GetField(value.ToString());
            var attributes = (DescriptionAttribute[])field.GetCustomAttributes(typeof(DescriptionAttribute), false);
            return attributes.Length > 0 ? attributes[0].Description : value.ToString();
        }
 
    }
}