/// <summary> /// 拷贝对象上的属性、值 /// </summary> public static void Clone(dynamic source, dynamic target) { if (source == null || target == null) { return; } DynamicX s = source as DynamicX; DynamicX t = target as DynamicX; if (s == null || t == null) { return; } foreach (var m in s.Members) { t.Set(m.Key, m.Value); } }
/// <summary> /// 把IDataReader的数据转换为动态类型 /// 注意:如果数据库字段名称为“new”或“_”开头,则在前面添加关键字"xly" /// </summary> public static dynamic ToDynamic(this IDataReader reader) { if (reader == null) { return(new ExpandoObject()); } int len = reader.FieldCount; if (len <= 0) { return(new ExpandoObject()); } DynamicX item = new DynamicX(); try { for (int i = 0; i < len; i++) { //过滤关键字 var name = reader.GetName(i); if (name.Equals("new") || name.StartsWith("_")) { name = name.Insert(0, "xly"); } object value = null; try { value = reader.GetValue(i); } catch {} item.TrySetMember(new SetPropertyBinder(name), value); } } catch { } //reaturn return(item); }