1
|
|
#region Copyright
|
2
|
|
|
3
|
|
|
4
|
|
|
5
|
|
|
6
|
|
|
7
|
|
|
8
|
|
|
9
|
|
|
10
|
|
|
11
|
|
|
12
|
|
|
13
|
|
|
14
|
|
|
15
|
|
|
16
|
|
|
17
|
|
#endregion
|
18
|
|
|
19
|
|
using System;
|
20
|
|
using System.Data.SqlTypes;
|
21
|
|
using System.Reflection;
|
22
|
|
using Nullables;
|
23
|
|
|
24
|
|
namespace Seasar.Framework.Util
|
25
|
|
{
|
26
|
|
|
27
|
|
|
28
|
|
|
29
|
|
public sealed class ConversionUtil
|
30
|
|
{
|
31
|
0
|
private ConversionUtil()
|
32
|
|
{
|
33
|
|
}
|
34
|
|
|
35
|
|
|
36
|
|
|
37
|
|
|
38
|
|
|
39
|
|
|
40
|
|
|
41
|
0
|
public static object ConvertTargetType(object o, Type targetType)
|
42
|
|
{
|
43
|
|
object ret = null;
|
44
|
|
|
45
|
|
if(typeof(IConvertible).IsAssignableFrom(targetType))
|
46
|
|
{
|
47
|
|
ret = ConvertConvertible(o, targetType);
|
48
|
|
}
|
49
|
|
else if(typeof(INullable).IsAssignableFrom(targetType))
|
50
|
|
{
|
51
|
|
ret = ConvertNullable(o, targetType);
|
52
|
|
}
|
53
|
|
else if(typeof(INullableType).IsAssignableFrom(targetType))
|
54
|
|
{
|
55
|
|
ret = ConvertNullableType(o, targetType);
|
56
|
|
}
|
57
|
|
else if(o == DBNull.Value)
|
58
|
|
{
|
59
|
|
ret = null;
|
60
|
|
}
|
61
|
|
else
|
62
|
|
{
|
63
|
|
ret = o;
|
64
|
|
}
|
65
|
|
|
66
|
|
return ret;
|
67
|
|
}
|
68
|
|
|
69
|
|
|
70
|
|
|
71
|
|
|
72
|
|
|
73
|
|
|
74
|
|
|
75
|
39
|
public static object ConvertNullable(object o, Type targetType)
|
76
|
|
{
|
77
|
39
|
INullable ret = null;
|
78
|
|
|
79
|
39
|
if(o == null || o == DBNull.Value)
|
80
|
|
{
|
81
|
26
|
ret = (INullable) targetType.InvokeMember("Null", BindingFlags.GetField,
|
82
|
|
null, null, null);
|
83
|
|
}
|
84
|
|
else
|
85
|
|
{
|
86
|
13
|
Type paramType = GetValueType(targetType);
|
87
|
13
|
ret = (INullable) Activator.CreateInstance(targetType,
|
88
|
|
new object[] { Convert.ChangeType(o, paramType) });
|
89
|
|
}
|
90
|
|
|
91
|
39
|
return ret;
|
92
|
|
}
|
93
|
|
|
94
|
|
|
95
|
|
|
96
|
|
|
97
|
|
|
98
|
|
|
99
|
|
|
100
|
36
|
public static object ConvertNullableType(object o, Type targetType)
|
101
|
|
{
|
102
|
36
|
INullableType ret = null;
|
103
|
|
|
104
|
36
|
if(o == null || o == DBNull.Value)
|
105
|
|
{
|
106
|
24
|
ret = (INullableType) targetType.InvokeMember("Default", BindingFlags.GetField,
|
107
|
|
null, null, null);
|
108
|
|
}
|
109
|
|
else
|
110
|
|
{
|
111
|
12
|
Type paramType = GetValueType(targetType);
|
112
|
12
|
ret = (INullableType) Activator.CreateInstance(targetType,
|
113
|
|
new object[] { Convert.ChangeType(o, paramType) });
|
114
|
|
}
|
115
|
|
|
116
|
36
|
return ret;
|
117
|
|
}
|
118
|
|
|
119
|
|
|
120
|
|
|
121
|
|
|
122
|
|
|
123
|
|
|
124
|
|
|
125
|
0
|
public static object ConvertConvertible(object o, Type targetType)
|
126
|
|
{
|
127
|
|
object ret = null;
|
128
|
|
|
129
|
|
if(o == null || o == DBNull.Value)
|
130
|
|
{
|
131
|
|
if(!targetType.Equals(typeof(string)))
|
132
|
|
{
|
133
|
|
ret = Convert.ChangeType(decimal.Zero, targetType);
|
134
|
|
}
|
135
|
|
}
|
136
|
|
else
|
137
|
|
{
|
138
|
|
ret = Convert.ChangeType(o, targetType);
|
139
|
|
}
|
140
|
|
|
141
|
|
return ret;
|
142
|
|
}
|
143
|
|
|
144
|
|
|
145
|
|
|
146
|
|
|
147
|
|
|
148
|
|
|
149
|
25
|
private static Type GetValueType(Type targetType)
|
150
|
|
{
|
151
|
25
|
PropertyInfo pi = targetType.GetProperty("Value");
|
152
|
25
|
return pi.PropertyType;
|
153
|
|
}
|
154
|
|
}
|
155
|
|
}
|