/// <summary> /// 获取具体大小 /// </summary> /// <param name="attr">特性</param> /// <param name="clazz">类型</param> /// <returns></returns> private static int GetSpecificSize(AdvancedObjectMarshalAsAttribute attr, Type clazz) { if (attr != null && attr.SizeConst > 0) { return(attr.SizeConst); } return(ValueTypeUtils.SizeBy(clazz)); }
/// <summary> /// 添加列表到流中 /// </summary> private static bool AddListToStream(Type clazz, IList value, AdvancedObjectMarshalAsAttribute attr, BinaryWriter bw) { Type element = TypeUtils.GetArrayElement(clazz); if (element != null) { if (value == null) { if (attr == null || attr.ArrayDoubleLength) { bw.Write(AdvancedObjectFormatter.As(BitConverter.GetBytes((short)-1), attr)); } else { bw.Write((sbyte)-1); } } else { int size = value.Count; if (attr != null && attr.SizeConst > 0) { size = attr.SizeConst; } else if (attr == null || attr.ArrayDoubleLength) { bw.Write(AdvancedObjectFormatter.As(BitConverter.GetBytes((short)size), attr)); } else { bw.Write((sbyte)size); } if (TypeUtils.IsBuffer(clazz)) { bw.Write((byte[])value); } else { bool basic = ValueTypeUtils.IsBasicType(element); for (int i = 0; i < size; i++) { object graph = value[i]; if (basic) { AdvancedObjectFormatter.AddValueToStream(element, graph, attr, bw); } else { AdvancedObjectFormatter.AddObjectToStream(graph, element, bw, attr); } } } } return(true); } return(false); }
/// <summary> /// 添加值到流中 /// </summary> private static bool AddValueToStream(Type clazz, object value, AdvancedObjectMarshalAsAttribute attr, BinaryWriter bw) { int size = ValueTypeUtils.SizeBy(clazz); if (size > 0) { if (ValueTypeUtils.IsFloatType(clazz)) { bw.Write(AdvancedObjectFormatter.As(ValueTypeUtils.BinaryBy(clazz, Convert.ToDouble(value)), attr)); } else { if (ValueTypeUtils.IsDateTime(clazz)) { value = Convert.ToDateTime(value).Ticks; } if (ValueTypeUtils.IsULong(clazz)) { value = unchecked ((long)(ulong)value); } if (ValueTypeUtils.IsIPAddress(clazz)) { value = BitConverter.ToInt32(((IPAddress)value).GetAddressBytes(), 0); } bw.Write(AdvancedObjectFormatter.As(ValueTypeUtils.BinaryBy(clazz, Convert.ToInt64(value), size), attr)); } return(true); } else { if (AdvancedObjectFormatter.AddStringToStream(clazz, value as string, attr, bw)) { return(true); } if (AdvancedObjectFormatter.AddListToStream(clazz, value as IList, attr, bw)) { return(true); } if (AdvancedObjectFormatter.AddObjectToStream(value, clazz, bw, attr)) { return(true); } } return(false); }
/// <summary> /// 将 DataTable 转换成数据模型集合 /// </summary> /// <typeparam name="T">数据模型类型</typeparam> /// <param name="value">DataSet</param> /// <returns></returns> public static IList <T> ToList <T>(this DataTable value) where T : class { if (value == null) { throw new ArgumentNullException(); } IList <KeyValuePair <PropertyInfo, int> > properties = new List <KeyValuePair <PropertyInfo, int> >(); foreach (PropertyInfo property in typeof(T).GetProperties(BindingFlags.Instance | BindingFlags.Public)) { foreach (DataColumn cols in value.Columns) { if ((cols.ColumnName).ToUpper() == (property.Name).ToUpper()) { properties.Add(new KeyValuePair <PropertyInfo, int>(property, cols.Ordinal)); } } } var ctor = typeof(T).GetConstructor(Type.EmptyTypes); IList <T> buffer = new List <T>(); foreach (DataRow row in value.Rows) { T model = (T)ctor.Invoke(null); try { buffer.Add(model); } finally { foreach (KeyValuePair <PropertyInfo, int> pair in properties) { object args = row[pair.Value]; if (args == DBNull.Value) { args = null; } Type prop = pair.Key.PropertyType; if (args != null && !prop.IsInstanceOfType(args)) { if (prop.IsEnum) { prop = prop.GetEnumUnderlyingType(); } if (ValueTypeUtils.IsFloatType(prop)) { args = ValueTypeFormatter.Parse(Convert.ToString(args), prop, NumberStyles.Number | NumberStyles.Float); } else { args = ValueTypeFormatter.Parse(Convert.ToString(args), prop); } } if (pair.Key.SetMethod != null) { pair.Key.SetValue(model, args, null); } } } } return(buffer); }