DataReader 数据流转为Dynamic对象。
通过 IDictionary 接口,可以非常方便的添加、修改、删除Dynamic的属性、方法、事件等。
1 /// <summary>
2 /// 将DataReader数据转为Dynamic对象
3 /// </summary>
4 /// <param name="reader"></param>
5 /// <returns></returns>
6 public static dynamic DataFillDynamic(IDataReader reader)
7 {
8 dynamic d = new ExpandoObject();
9 for (int i = 0; i < reader.FieldCount; i++)
10 {
11 try
12 {
13 ((IDictionary<string, object>)d).Add(reader.GetName(i), reader.GetValue(i));
14 }
15 catch
16 {
17 ((IDictionary<string, object>)d).Add(reader.GetName(i), null);
18 }
19 }
20 return d;
21 }
22
2 /// 将DataReader数据转为Dynamic对象
3 /// </summary>
4 /// <param name="reader"></param>
5 /// <returns></returns>
6 public static dynamic DataFillDynamic(IDataReader reader)
7 {
8 dynamic d = new ExpandoObject();
9 for (int i = 0; i < reader.FieldCount; i++)
10 {
11 try
12 {
13 ((IDictionary<string, object>)d).Add(reader.GetName(i), reader.GetValue(i));
14 }
15 catch
16 {
17 ((IDictionary<string, object>)d).Add(reader.GetName(i), null);
18 }
19 }
20 return d;
21 }
22