-
Notifications
You must be signed in to change notification settings - Fork 0
/
simpleDTO.cs
120 lines (110 loc) · 4.16 KB
/
simpleDTO.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
/// <summary>
/// 提供将SqlDataReader转成T类型的扩展方法
/// </summary>
public static class SqlDataReaderEx
{
private static object _obj = new object();
/// <summary>
/// 属性反射信息缓存 key:类型的hashCode,value属性信息
/// </summary>
private static Dictionary<int, Dictionary<string, PropertyInfo>> propInfoCache = new Dictionary<int, Dictionary<string, PropertyInfo>>();
/// <summary>
/// 将SqlDataReader转成T类型
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="reader"></param>
/// <returns></returns>
public static T To<T>(this SqlDataReader reader)
where T : new()
{
if (reader == null || reader.HasRows == false) return default(T);
var res = new T();
var propInfos = GetFieldnameFromCache<T>();
for (int i = 0; i < reader.FieldCount; i++)
{
var n = reader.GetName(i).ToLower();
if (propInfos.ContainsKey(n))
{
PropertyInfo prop = propInfos[n];
var IsValueType = prop.PropertyType.IsValueType;
object defaultValue = null;//引用类型或可空值类型的默认值
if (IsValueType) {
if ((!prop.PropertyType.IsGenericType)
||(prop.PropertyType.IsGenericType&&!prop.PropertyType.GetGenericTypeDefinition().Equals(typeof(Nullable<>))))
{
defaultValue = 0;//非空值类型的默认值
}
}
var v = reader.GetValue(i);
prop.SetValue(res, (Convert.IsDBNull(v) ? defaultValue : v), null);
}
}
return res;
}
private static Dictionary<string, PropertyInfo> GetFieldnameFromCache<T>()
{
Dictionary<string, PropertyInfo> res = null;
var hashCode = typeof(T).GetHashCode();
var filedNames = GetFieldname<T>();
lock (_obj)
{
if (!propInfoCache.ContainsKey(hashCode))
{
propInfoCache.Add(hashCode, filedNames);
}
}
res = propInfoCache[hashCode];
return res;
}
/// <summary>
/// 获取一个类型的对应数据表的字段信息
/// </summary>
/// <typeparam name="T"></typeparam>
/// <returns></returns>
private static Dictionary<string, PropertyInfo> GetFieldname<T>()
{
var res = new Dictionary<string, PropertyInfo>();
var props = typeof(T).GetProperties();
foreach (PropertyInfo item in props)
{
res.Add(item.GetFieldName(), item);
}
return res;
}
/// <summary>
/// 将SqlDataReader转成List<T>类型
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="reader"></param>
/// <returns></returns>
public static List<T> ToList<T>(this SqlDataReader reader)
where T : new()
{
if (reader == null || reader.HasRows == false) return null;
var res = new List<T>();
while (reader.Read())
{
res.Add(reader.To<T>());
}
return res;
}
/// <summary>
/// 获取该属性对应到数据表中的字段名称
/// </summary>
/// <param name="propInfo"></param>
/// <returns></returns>
public static string GetFieldName(this PropertyInfo propInfo)
{
var fieldname = propInfo.Name;
var attr = propInfo.GetCustomAttributes(false);
foreach (var a in attr)
{
if (a is DataFieldAttribute)
{
fieldname = (a as DataFieldAttribute).Name;
break;
}
}
return fieldname.ToLower();
}
}