forked from cyq1162/cyqdata
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathConvertTool.cs
243 lines (231 loc) · 8.47 KB
/
ConvertTool.cs
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
using CYQ.Data.SQL;
using CYQ.Data.Table;
using System;
using System.Collections.Generic;
using System.Data.SqlTypes;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using System.Text;
using System.Web;
namespace CYQ.Data.Tool
{
/// <summary>
/// 类型转换(支持json转实体)
/// </summary>
public static class ConvertTool
{
/// <summary>
/// 类型转换(精准强大)
/// </summary>
/// <param name="value">值处理</param>
/// <param name="t">类型</param>
/// <returns></returns>
public static object ChangeType(object value, Type t)
{
if (t == null) { return null; }
string strValue = Convert.ToString(value);
if (t.IsEnum)
{
if (strValue != "")
{
if (Enum.IsDefined(t, strValue))
{
return Enum.Parse(t, strValue);
}
int v = 0;
if (int.TryParse(strValue, out v))
{
object v1 = Enum.Parse(t, strValue);
if (v1.ToString() != strValue)
{
return v1;
}
}
string[] names = Enum.GetNames(t);
string lower = strValue.ToLower();
foreach (string name in names)
{
if (name.ToLower() == lower)
{
return Enum.Parse(t, name);
}
}
}
//取第一个值。
string firstKey = Enum.GetName(t, -1);
if (!string.IsNullOrEmpty(firstKey))
{
return Enum.Parse(t, firstKey);
}
return Enum.Parse(t, Enum.GetNames(t)[0]);
}
if (value == null)
{
return t.IsValueType ? Activator.CreateInstance(t) : null;
}
if (t.FullName == "System.Object")
{
return value;
}
if (t.FullName == "System.Type")
{
return (Type)value;
}
if (t.FullName == "System.IO.Stream" && value is HttpPostedFile)
{
return ((HttpPostedFile)value).InputStream;
}
if (t.IsGenericType && t.Name.StartsWith("Nullable"))
{
t = Nullable.GetUnderlyingType(t);
if (strValue == "")
{
return null;
}
}
if (t.Name == "String")
{
if (value is byte[])
{
return Convert.ToBase64String((byte[])value);
}
return strValue;
}
if (t.FullName == "System.Text.StringBuilder")
{
return value as StringBuilder;
}
if (t.FullName == "System.Text.Encoding")
{
return value as Encoding;
}
if (strValue.Trim() == "")
{
if (t.Name.EndsWith("[]")) { return null; }
return Activator.CreateInstance(t);
}
else if (t.IsValueType)
{
if (t.Name == "DateTime")
{
switch (strValue.ToLower().TrimEnd(')', '('))
{
case "now":
case "getdate":
case "current_timestamp":
return DateTime.Now;
}
if (DateTime.Parse(strValue) == DateTime.MinValue)
{
return (DateTime)SqlDateTime.MinValue;
}
return Convert.ChangeType(value, t);//这里用value,避免丢失毫秒
}
else if (t.Name == "Guid")
{
if (strValue == SqlValue.Guid || strValue.StartsWith("newid"))
{
return Guid.NewGuid();
}
else if (strValue.ToLower() == "null")
{
return Guid.Empty;
}
return new Guid(strValue);
}
else
{
switch (strValue.ToLower())
{
case "yes":
case "true":
case "1":
case "on":
case "是":
if (t.Name == "Boolean")
return true;
else strValue = "1";
break;
case "no":
case "false":
case "0":
case "":
case "否":
case "null":
if (t.Name == "Boolean")
return false;
else strValue = "0";
break;
case "infinity":
case "正无穷大":
if (t.Name == "Double" || t.Name == "Single")
return double.PositiveInfinity;
break;
case "-infinity":
case "负无穷大":
if (t.Name == "Double" || t.Name == "Single")
return double.NegativeInfinity;
break;
default:
if (t.Name == "Boolean")
return false;
break;
}
if (t.Name.StartsWith("Int") || t.Name == "Byte")
{
if (strValue.IndexOf('.') > -1)//11.22
{
strValue = strValue.Split('.')[0];
}
else if (value.GetType().IsEnum)
{
return (int)value;
}
}
}
return Convert.ChangeType(strValue, t);
}
else
{
Type valueType = value.GetType();
//if(valueType.IsEnum && t.is)
if (valueType.FullName != t.FullName)
{
switch (ReflectTool.GetSystemType(ref t))
{
case SysType.Custom:
return MDataRow.CreateFrom(strValue).ToEntity(t);
case SysType.Generic:
if (t.Name.StartsWith("List"))
{
return MDataTable.CreateFrom(strValue).ToList(t);
}
break;
case SysType.Array:
if (t.Name == "Byte[]")
{
if (valueType.Name == "String")
{
return Convert.FromBase64String(strValue);
}
using (MemoryStream ms = new MemoryStream())
{
new BinaryFormatter().Serialize(ms, value);
return ms.ToArray();
}
}
break;
}
}
return Convert.ChangeType(value, t);
}
}
/// <summary>
/// 类型转换(精准强大)
/// </summary>
public static T ChangeType<T>(object value)
{
return (T)ChangeType(value, typeof(T));
}
}
}