-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConvertor.cs
More file actions
144 lines (141 loc) · 4.14 KB
/
Convertor.cs
File metadata and controls
144 lines (141 loc) · 4.14 KB
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
using System;
using System.Collections.Generic;
using System.Text;
namespace CodeGenerate.Common
{
public static class Convertor
{
#region 数据格式转换
/// <summary>
/// 转换成Int
/// </summary>
/// <param name="inputValue">输入值</param>
/// <returns></returns>
public static int ToInt(this object inputValue)
{
if (inputValue.IsInt())
{
return int.Parse(inputValue.ToStringValue());
}
return 0;
}
/// <summary>
/// 转换成Int32
/// </summary>
/// <param name="inputValue">输入值</param>
/// <returns></returns>
public static int ToInt32(this object inputValue)
{
if (inputValue.IsInt())
{
return Convert.ToInt32(inputValue);
}
return 0;
}
/// <summary>
/// 转换成Int64
/// </summary>
/// <param name="inputValue">输入值</param>
/// <returns></returns>
public static long ToInt64(this object inputValue)
{
if (inputValue.IsInt())
{
return Convert.ToInt64(inputValue);
}
return 0;
}
/// <summary>
/// 转换成Double
/// </summary>
/// <param name="inputValue">输入值</param>
/// <returns></returns>
public static Double ToDouble(this object inputValue)
{
if (!inputValue.IsNullOrEmpty())
{
return Convert.ToDouble(inputValue);
}
return 0;
}
/// <summary>
/// 转换成Decimal
/// </summary>
/// <param name="inputValue">输入值</param>
/// <returns></returns>
public static Decimal ToDecimal(this object inputValue)
{
if (!inputValue.IsNullOrEmpty())
{
return Convert.ToDecimal(inputValue);
}
return 0;
}
/// <summary>
/// 转换成Int64
/// </summary>
/// <param name="inputValue">输入值</param>
/// <returns></returns>
public static bool? ToBool(this object inputValue)
{
if (inputValue.IsNullOrEmpty())
{
return null;
}
return bool.Parse(inputValue.ToStringValue());
}
/// <summary>
/// 转换obj 成string
/// </summary>
/// <param name="inputValue">object</param>
/// <returns></returns>
public static string ToStringValue(this object inputValue)
{
if (inputValue == null)
{
return "";
}
else
{
return inputValue.ToString();
}
}
/// <summary>
/// 转换成数据库字符串
/// </summary>
/// <param name="inputValue">输入值</param>
/// <returns></returns>
public static string ToDBString(this object inputValue)
{
if (!inputValue.IsNullOrEmpty())
{
return ("'" + inputValue.ToStringValue().Trim().Replace("'", "''") + "'");
}
return "''";
}
/// <summary>
/// 转换成时间类型yyyy-MM-dd
/// </summary>
/// <param name="inputValue">输入值</param>
/// <returns></returns>
public static DateTime ToDateTime(this object inputValue)
{
if (inputValue.IsNullOrEmpty())
{
return DateTime.MinValue;
}
return Convert.ToDateTime(inputValue);
}
/// <summary>
/// 转换成时间类型
/// </summary>
/// <param name="inputValue">输入值</param>
/// <param name="format">时间格式</param>
/// <returns></returns>
public static DateTime ToDateTime(this object inputValue, string format)
{
return DateTime.ParseExact(inputValue.ToStringValue(), format, null);
}
#endregion
}
}