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;
|
21
|
|
using System.Reflection;
|
22
|
|
using Seasar.Framework.Util;
|
23
|
|
|
24
|
|
namespace Seasar.Extension.Unit
|
25
|
|
{
|
26
|
|
public class BeanReader : Seasar.Extension.DataSets.IDataReader
|
27
|
|
{
|
28
|
|
private DataSet dataSet_;
|
29
|
|
|
30
|
|
private DataTable table_;
|
31
|
|
|
32
|
1
|
protected BeanReader() : this(null)
|
33
|
|
{
|
34
|
|
}
|
35
|
|
|
36
|
14
|
public BeanReader(object bean)
|
37
|
|
{
|
38
|
14
|
dataSet_ = new DataSet();
|
39
|
14
|
table_ = dataSet_.Tables.Add("Bean");
|
40
|
|
|
41
|
14
|
if (bean != null)
|
42
|
|
{
|
43
|
13
|
Type beanType = bean.GetType();
|
44
|
13
|
SetupColumns(beanType);
|
45
|
13
|
SetupRow(beanType, bean);
|
46
|
|
}
|
47
|
|
}
|
48
|
|
|
49
|
14
|
protected void SetupColumns(Type beanType)
|
50
|
|
{
|
51
|
14
|
foreach (PropertyInfo pi in beanType.GetProperties())
|
52
|
|
{
|
53
|
106
|
Type propertyType = PropertyUtil.GetPrimitiveType(pi.PropertyType);
|
54
|
106
|
table_.Columns.Add(pi.Name, propertyType);
|
55
|
|
}
|
56
|
|
}
|
57
|
|
|
58
|
14
|
protected void SetupRow(Type beanType, object bean)
|
59
|
|
{
|
60
|
14
|
DataRow row = table_.NewRow();
|
61
|
14
|
foreach (PropertyInfo pi in beanType.GetProperties())
|
62
|
|
{
|
63
|
106
|
object value = pi.GetValue(bean, null);
|
64
|
106
|
row[pi.Name] = PropertyUtil.GetPrimitiveValue(value);
|
65
|
|
}
|
66
|
14
|
table_.Rows.Add(row);
|
67
|
14
|
row.AcceptChanges();
|
68
|
|
}
|
69
|
|
|
70
|
|
#region IDataReader メンバ
|
71
|
|
|
72
|
14
|
public DataSet Read()
|
73
|
|
{
|
74
|
14
|
return dataSet_;
|
75
|
|
}
|
76
|
|
|
77
|
|
#endregion
|
78
|
|
}
|
79
|
|
}
|
80
|
|
|