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
|
|
public sealed class PropertyUtil
|
27
|
|
{
|
28
|
0
|
private PropertyUtil()
|
29
|
|
{
|
30
|
|
}
|
31
|
|
|
32
|
179
|
public static Type GetPrimitiveType(Type type)
|
33
|
|
{
|
34
|
179
|
if (type.GetInterface(typeof(INullableType).Name) != null)
|
35
|
|
{
|
36
|
44
|
ConstructorInfo[] constructorInfos = type.GetConstructors();
|
37
|
44
|
ParameterInfo[] parameterInfos = constructorInfos[0].GetParameters();
|
38
|
44
|
type = parameterInfos[0].ParameterType;
|
39
|
|
}
|
40
|
135
|
else if (type.GetInterface(typeof(INullable).Name) != null)
|
41
|
|
{
|
42
|
48
|
PropertyInfo npi = type.GetProperty("Value");
|
43
|
48
|
return npi.PropertyType;
|
44
|
|
}
|
45
|
87
|
else if (type.IsGenericType && type.GetGenericTypeDefinition() == typeof(Nullable<>))
|
46
|
|
{
|
47
|
22
|
type = Nullable.GetUnderlyingType(type);
|
48
|
|
}
|
49
|
131
|
return type;
|
50
|
|
}
|
51
|
|
|
52
|
180
|
public static object GetPrimitiveValue(object value)
|
53
|
|
{
|
54
|
180
|
if (value == null || value == DBNull.Value)
|
55
|
|
{
|
56
|
14
|
return DBNull.Value;
|
57
|
|
}
|
58
|
|
|
59
|
166
|
if (value is INullableType)
|
60
|
|
{
|
61
|
44
|
INullableType nullableType = (INullableType) value;
|
62
|
44
|
if (nullableType.HasValue)
|
63
|
|
{
|
64
|
22
|
return nullableType.Value;
|
65
|
|
}
|
66
|
|
else
|
67
|
|
{
|
68
|
22
|
return DBNull.Value;
|
69
|
|
}
|
70
|
|
}
|
71
|
122
|
else if (value is INullable)
|
72
|
|
{
|
73
|
48
|
INullable nullable = (INullable) value;
|
74
|
48
|
if (!nullable.IsNull)
|
75
|
|
{
|
76
|
26
|
PropertyInfo npi = value.GetType().GetProperty("Value");
|
77
|
26
|
return npi.GetValue(value, null);
|
78
|
|
}
|
79
|
|
else
|
80
|
|
{
|
81
|
22
|
return DBNull.Value;
|
82
|
|
}
|
83
|
|
}
|
84
|
|
|
85
|
74
|
return value;
|
86
|
|
}
|
87
|
|
}
|
88
|
|
}
|
89
|
|
|