public static MDataColumn GetColumns(Type typeInfo) { string key = "ColumnCache_" + typeInfo.FullName; if (_ColumnCache.ContainsKey(key)) { return(_ColumnCache[key].Clone()); } else { #region 获取列结构 MDataColumn mdc = new MDataColumn(); mdc.TableName = typeInfo.Name; switch (StaticTool.GetSystemType(ref typeInfo)) { case SysType.Base: case SysType.Enum: mdc.Add(typeInfo.Name, DataType.GetSqlType(typeInfo), false); return(mdc); case SysType.Generic: case SysType.Collection: Type[] argTypes; Tool.StaticTool.GetArgumentLength(ref typeInfo, out argTypes); foreach (Type type in argTypes) { mdc.Add(type.Name, DataType.GetSqlType(type), false); } argTypes = null; return(mdc); } List <PropertyInfo> pis = StaticTool.GetPropertyInfo(typeInfo); if (pis.Count > 0) { for (int i = 0; i < pis.Count; i++) { SetStruct(mdc, pis[i], null, i, pis.Count); } } else { List <FieldInfo> fis = StaticTool.GetFieldInfo(typeInfo); if (fis.Count > 0) { for (int i = 0; i < fis.Count; i++) { SetStruct(mdc, null, fis[i], i, fis.Count); } } } object[] tableAttr = typeInfo.GetCustomAttributes(typeof(DescriptionAttribute), false);//看是否设置了表特性,获取表名和表描述 if (tableAttr != null && tableAttr.Length == 1) { DescriptionAttribute attr = tableAttr[0] as DescriptionAttribute; if (attr != null && !string.IsNullOrEmpty(attr.Description)) { mdc.Description = attr.Description; } } pis = null; #endregion if (!_ColumnCache.ContainsKey(key)) { _ColumnCache.Set(key, mdc.Clone()); } return(mdc); } }
public static MDataColumn GetColumns(Type typeInfo) { string key = "ColumnCache:" + typeInfo.FullName; if (columnCache.ContainsKey(key)) { return(columnCache[key].Clone()); } else { #region 获取列结构 MDataColumn mdc = new MDataColumn(); switch (StaticTool.GetSystemType(ref typeInfo)) { case SysType.Base: case SysType.Enum: mdc.Add(typeInfo.Name, DataType.GetSqlType(typeInfo), false); return(mdc); case SysType.Generic: case SysType.Collection: Type[] argTypes; Tool.StaticTool.GetArgumentLength(ref typeInfo, out argTypes); foreach (Type type in argTypes) { mdc.Add(type.Name, DataType.GetSqlType(type), false); } argTypes = null; return(mdc); } PropertyInfo[] pis = StaticTool.GetPropertyInfo(typeInfo); SqlDbType sqlType; for (int i = 0; i < pis.Length; i++) { sqlType = SQL.DataType.GetSqlType(pis[i].PropertyType); mdc.Add(pis[i].Name, sqlType); MCellStruct column = mdc[i]; column.MaxSize = DataType.GetMaxSize(sqlType); if (i == 0) { column.IsPrimaryKey = true; column.IsCanNull = false; if (column.ColumnName.ToLower().Contains("id") && (column.SqlType == System.Data.SqlDbType.Int || column.SqlType == SqlDbType.BigInt)) { column.IsAutoIncrement = true; } } else if (i > pis.Length - 3 && sqlType == SqlDbType.DateTime && pis[i].Name.EndsWith("Time")) { column.DefaultValue = SqlValue.GetDate; } } pis = null; #endregion if (!columnCache.ContainsKey(key)) { columnCache.Add(typeInfo.FullName, mdc.Clone()); } return(mdc); } }
/// <summary> /// 序列化 /// </summary> /// <param name="value">值</param> /// <param name="type">返回序列化类型</param> /// <param name="compressionThreshold">指定超过长度时启用压缩功能</param> /// <returns></returns> public static byte[] Serialize(object value, out SerializedType type, uint compressionThreshold) { byte[] bytes; if (value is byte[]) { bytes = (byte[])value; type = SerializedType.ByteArray; if (bytes.Length > compressionThreshold) { bytes = compress(bytes); type = SerializedType.CompressedByteArray; } } else if (value is string) { bytes = Encoding.UTF8.GetBytes((string)value); type = SerializedType.String; if (bytes.Length > compressionThreshold) { bytes = compress(bytes); type = SerializedType.CompressedString; } } else if (value is DateTime) { bytes = BitConverter.GetBytes(((DateTime)value).Ticks); type = SerializedType.Datetime; } else if (value is bool) { bytes = new byte[] { (byte)((bool)value ? 1 : 0) }; type = SerializedType.Bool; } else if (value is byte) { bytes = new byte[] { (byte)value }; type = SerializedType.Byte; } else if (value is short) { bytes = BitConverter.GetBytes((short)value); type = SerializedType.Short; } else if (value is ushort) { bytes = BitConverter.GetBytes((ushort)value); type = SerializedType.UShort; } else if (value is int) { bytes = BitConverter.GetBytes((int)value); type = SerializedType.Int; } else if (value is uint) { bytes = BitConverter.GetBytes((uint)value); type = SerializedType.UInt; } else if (value is long) { bytes = BitConverter.GetBytes((long)value); type = SerializedType.Long; } else if (value is ulong) { bytes = BitConverter.GetBytes((ulong)value); type = SerializedType.ULong; } else if (value is float) { bytes = BitConverter.GetBytes((float)value); type = SerializedType.Float; } else if (value is double) { bytes = BitConverter.GetBytes((double)value); type = SerializedType.Double; } else { type = SerializedType.ObjectJson; Type t = value.GetType(); switch (StaticTool.GetSystemType(ref t)) { case SysType.Base: case SysType.Enum: case SysType.Custom: if (t.GetCustomAttributes(typeof(SerializableAttribute), false).Length > 0) { type = SerializedType.Object; } break; } if (type == SerializedType.Object) { //可序列化 List<object> 如果 object 不支持序列化,会挂。 using (MemoryStream ms = new MemoryStream()) { new BinaryFormatter().Serialize(ms, value); bytes = ms.ToArray(); if (bytes.Length > compressionThreshold) { bytes = compress(bytes); type = SerializedType.CompressedObject; } } } else { string json = JsonHelper.ToJson(value); bytes = Encoding.UTF8.GetBytes(json); if (bytes.Length > compressionThreshold) { bytes = compress(bytes); type = SerializedType.CompressedObjectJson; } } } return(bytes); }