Moli
2022-04-15 22477f0eca904ec628ab59cd749f959af35c1439
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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Text;
 
namespace Helpers
{
    static public class MyConvert
    {
        /// <summary>
        /// 其他类型转json字符串
        /// </summary>
        /// <param name="obj">待转换的对象</param>
        /// <returns></returns>
        public static string ToJson(this object obj)
        {
            if (obj == null)
            {
                return null;
            }
            return Newtonsoft.Json.JsonConvert.SerializeObject(obj);
        }
        /// <summary>
        /// json转model
        /// </summary>
        /// <typeparam name="T">转换后的类型</typeparam>
        /// <param name="str">json字符串</param>
        /// <returns></returns>
        public static T ToModel<T>(this string str)
        {
            if (!string.IsNullOrWhiteSpace(str))
            {
                var res = Newtonsoft.Json.JsonConvert.DeserializeObject<T>(str);
                if (res == null) {
                    return default;
                } else 
                {
                    return res;
                }
            }
            return default;
        }
        /// <summary>
        /// 其他类型转model
        /// </summary>
        /// <typeparam name="T">转换后的对象</typeparam>
        /// <param name="obj">待转换的对象</param>
        /// <returns></returns>
        public static T ToModel<T>(this object obj)
        {
            if (obj == null)
            {
                return default(T);
            }
            string json = Newtonsoft.Json.JsonConvert.SerializeObject(obj);
            T model = Newtonsoft.Json.JsonConvert.DeserializeObject<T>(json);
            return model;
        }
        /// <summary>
        /// 其他类型转DataTable
        /// </summary>
        /// <param name="obj">待转换的对象</param>
        /// <returns></returns>
        public static DataTable ToTable(this object obj)
        {
            if (obj == null)
            {
                return null;
            }
            string json = Newtonsoft.Json.JsonConvert.SerializeObject(obj);
            return Newtonsoft.Json.JsonConvert.DeserializeObject<DataTable>(json);
        }
        /// <summary>
        /// json转DataTable
        /// </summary>
        /// <param name="str">json字符串</param>
        /// <returns></returns>
        public static DataTable ToTable(this string str)
        {
            if (!string.IsNullOrWhiteSpace(str))
            {
                return Newtonsoft.Json.JsonConvert.DeserializeObject<DataTable>(str);
            }
            return default(DataTable);
        }
        /// <summary>
        /// 字符串转整形
        /// </summary>
        /// <param name="str">数值字符串</param>
        /// <returns></returns>
        /// <exception cref="Exception">异常:非法的数据</exception>
        public static int ToInt(this string str)
        {
            int resInt = 0;
            if (int.TryParse(str, out resInt))
            {
                return resInt;
            }
            else
            {
                throw new Exception("该字符串无法转换成整形");
            }
        }
        /// <summary>
        /// 其他类型转整形
        /// </summary>
        /// <param name="str">数值对象</param>
        /// <returns></returns>
        /// <exception cref="Exception">异常:非法的数据</exception>
        public static int ToInt(this object str)
        {
            int resInt = 0;
            if (int.TryParse(str.ToString(), out resInt))
            {
                return resInt;
            }
            else
            {
                throw new Exception("该字符串无法转换成整形");
            }
        }
        /// <summary>
        /// datetime转字符串(yyyy-MM-dd HH:mm:ss.fff)
        /// </summary>
        /// <param name="dt">待转换的时间</param>
        /// <returns></returns>
        public static string GetTime(this DateTime dt)
        {
            return dt.ToString("yyyy-MM-dd HH:mm:ss.fff");
        }
        /// <summary>
        /// 其他类型转时间
        /// </summary>
        /// <param name="obj">待转换的对象</param>
        /// <returns></returns>
        /// <exception cref="Exception"></exception>
        public static DateTime ToDateTime(this object obj)
        {
            if (obj == null)
            {
                throw new Exception("对象不能为空");
            }
            string str = obj.ToString();
            DateTime dt = DateTime.Now;
            if (DateTime.TryParse(str, out dt))
            {
                return dt;
            }
            else
            {
                throw new Exception("非法字符串");
            }
        }
        /// <summary>
        /// 字符串转时间
        /// </summary>
        /// <param name="str">待转换的字符串对象</param>
        /// <returns></returns>
        public static DateTime FormatDate(this string str)
        {
            return DateTime.Parse(str);
        }
        /// <summary>
        /// 时间转固定格式字符串
        /// </summary>
        /// <param name="dt">等转换的时间</param>
        /// <returns></returns>
        public static string ToTimeStr(this DateTime dt) 
        {
            return GetTime(dt);
        }
        /// <summary>
        /// 对象转日期字符串(yyyy-MM-dd)
        /// </summary>
        /// <param name="obj">待转换的对象</param>
        /// <returns></returns>
        /// <exception cref="Exception"></exception>
        public static string GetDate(this object obj)
        {
            if (obj == null)
            {
                return null;
            }
            string str = obj.ToString();
            DateTime dt = DateTime.Now;
            if (DateTime.TryParse(str, out dt))
            {
                return dt.ToString("yyyy-MM-dd");
            }
            else
            {
                throw new Exception("无法转换的内容");
            }
        }
        /// <summary>
        /// 流转Byte数组
        /// </summary>
        /// <param name="stream">待转换的流</param>
        /// <returns></returns>
        public static byte[] ToByte(this Stream stream)
        {
            byte[] bytes = new byte[stream.Length];
            stream.Read(bytes, 0, bytes.Length);
 
            // 设置当前流的位置为流的开始 
            stream.Seek(0, SeekOrigin.Begin);
            return bytes;
        }
        /// <summary>
        /// Byte数组转流
        /// </summary>
        /// <param name="bytes"></param>
        /// <returns></returns>
        public static Stream ToStream(this byte[] bytes)
        {
            Stream stream = new MemoryStream(bytes);
            return stream;
        }
        /// <summary>
        /// json转list数组
        /// </summary>
        /// <typeparam name="T">List<T>中的泛型对象类型</typeparam>
        /// <param name="str">待转换的json字符串</param>
        /// <returns></returns>
        public static List<T> ToList<T>(this string str)
        {
            if (!string.IsNullOrWhiteSpace(str))
            {
                return Newtonsoft.Json.JsonConvert.DeserializeObject<List<T>>(str);
            }
            return default(List<T>);
        }
        /// <summary>
        /// 其他类型转List
        /// </summary>
        /// <typeparam name="T">List<T>中的泛型对象类型</typeparam>
        /// <param name="obj">待转换的对象</param>
        /// <returns></returns>
        public static List<T> ToList<T>(this object obj)
        {
            string str = obj.ToJson();
            if (!string.IsNullOrWhiteSpace(str))
            {
                return Newtonsoft.Json.JsonConvert.DeserializeObject<List<T>>(str);
            }
            return default(List<T>);
        }
        /// <summary>
        /// DataTable首列转List<string>
        /// </summary>
        /// <param name="dt">待转换的Datatable</param>
        /// <returns></returns>
        public static List<string> ToList(this DataTable dt)
        {
            List<string> res = new List<string>();
            if (dt == null || dt.Rows == null || dt.Rows.Count == 0) { return res; }
            foreach (DataRow dr in dt.Rows)
            {
                if (string.IsNullOrWhiteSpace(Convert.ToString(dr[0]))) { continue; }
                res.Add(Convert.ToString(dr[0]));
            }
            return res;
        }
        /// <summary>
        /// 将List<string>转成DataTable
        /// </summary>
        /// <param name="list">新列的数据</param>
        /// <param name="ColumnName">列名</param>
        /// <returns></returns>
        public static DataTable ToTable(this List<string> list, string ColumnName)
        {
            DataTable res = new DataTable();
            res.Columns.Add(ColumnName);
            if (list == null || list.Count == 0) { return res; }
            foreach (var a in list)
            {
                if (string.IsNullOrWhiteSpace(a)) { continue; }
                var dr = res.NewRow();
                dr[0] = a;
                res.Rows.Add(dr);
            }
            return res;
        }
        /// <summary>
        /// 将DataTable中的空行移除
        /// </summary>
        /// <param name="dt">待处理的DataTable</param>
        /// <returns></returns>
        public static DataTable RemoveEmpty(this DataTable dt)
        {
            int count = dt.Rows.Count - 1;
            while (count >= 0)
            {
                bool b = true;
                foreach (var a in dt.Rows[count].ItemArray)
                {
                    if (!string.IsNullOrWhiteSpace(a.ToString()))
                    {
                        b = b & false;
                        break;
                    }
                }
                if (b)
                {
                    dt.Rows.Remove(dt.Rows[count]);
                }
                count--;
            }
            return dt;
        }
    }
}