-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathResultSetMapper.cs
48 lines (46 loc) · 1.53 KB
/
ResultSetMapper.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
public class MyReflectionResultSetMapper<T> : IResultSetMapper<T> where T : new()
{
public MyReflectionResultSetMapper()
{
var t = typeof(T);
var props = t.GetProperties();
var d = new Dictionary<string, PropertyInfo>();
foreach (var propertyInfo in props)
{
d.Add(propertyInfo.Name.ToLower(), propertyInfo);
}
dic = d;
}
protected virtual string GetPropertyNameFromFieldName(string fieldName)
{
return fieldName;
}
private readonly IReadOnlyDictionary<string, PropertyInfo> dic;
public IEnumerable<T> MapSet(IDataReader reader)
{
Dictionary<int,PropertyInfo> dicn=new Dictionary<int, PropertyInfo>();
for (int i = 0; i < reader.FieldCount; i++)
{
var name = reader.GetName(i);
var n = GetPropertyNameFromFieldName(name).ToLower();
if (dic.ContainsKey(n))
{
dicn[i] = dic[n];
}
}
while (reader.Read())
{
T obj = new T();
foreach (var key in dicn.Keys)
{
var p = dicn[key];
//Work with null
if (!reader.IsDBNull(key))
{
p.SetValue(obj, reader[key]);
}
}
yield return obj;
}
}
}