/// <summary> /// 是否为基元类型 /// </summary> /// <param name="flag"></param> /// <returns></returns> protected bool IsTargetType(byte flag, out TargetTypeFlag @enum) { @enum = 0; if (!Enum.IsDefined(typeof(TargetTypeFlag), flag)) { return(false); } @enum = (TargetTypeFlag)flag; return(true); }
/// <summary> /// 是否为基元类型 /// </summary> /// <param name="type"></param> /// <returns></returns> protected bool IsNullableTargetType(Type type, out TargetTypeFlag @enum) { @enum = 0; var targetType = Nullable.GetUnderlyingType(type); if (targetType != null) { return(this.IsTargetType(targetType, out @enum)); } return(false); }
/// <summary> /// 是否为基元类型 /// </summary> /// <param name="type"></param> /// <returns></returns> protected bool IsTargetType(Type type, out TargetTypeFlag @enum) { @enum = 0; var targetType = type; if (TypeHelper.IsPrimitiveType(targetType)) { @enum = TargetSorted[targetType]; return(true); } if (targetType == typeof(DateTime) || targetType == typeof(TimeSpan) || targetType == typeof(decimal)) { @enum = TargetSorted[targetType]; return(true); } return(false); }
public bool HasFlag(TargetTypeFlag flag) { return (TargetFlags & (int)flag) > 0; }
/// <summary> /// 一些基元类型解压缩 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="byte"></param> /// <param name="flag"></param> /// <param name="value"></param> /// <returns></returns> protected bool TryDecompressTargetTypeFlag <T>(byte[] @byte, byte flag, TargetTypeFlag enumFlag, out T value) { value = default(T); try { switch (enumFlag) { case TargetTypeFlag.@char: { value = (T)(dynamic)BitConverter.ToChar(@byte, 0); return(true); } case TargetTypeFlag.@bool: { value = (T)(dynamic)BitConverter.ToBoolean(@byte, 0); return(true); } case TargetTypeFlag.@byte: { value = (T)(dynamic)((byte)BitConverter.ToInt16(@byte, 0)); return(true); } case TargetTypeFlag.@short: { value = (T)(dynamic)BitConverter.ToInt16(@byte, 0); return(true); } case TargetTypeFlag.@ushort: { value = (T)(dynamic)BitConverter.ToUInt16(@byte, 0); return(true); } case TargetTypeFlag.@int: { value = (T)(dynamic)BitConverter.ToInt32(@byte, 0); return(true); } case TargetTypeFlag.@uint: { value = (T)(dynamic)BitConverter.ToUInt32(@byte, 0); return(true); } case TargetTypeFlag.@long: { value = (T)(dynamic)BitConverter.ToInt64(@byte, 0); return(true); } case TargetTypeFlag.@ulong: { value = (T)(dynamic)BitConverter.ToUInt64(@byte, 0); return(true); } case TargetTypeFlag.@float: { value = (T)(dynamic)BitConverter.ToSingle(@byte, 0); return(true); } case TargetTypeFlag.@double: { value = (T)(dynamic)BitConverter.ToDouble(@byte, 0); return(true); } case TargetTypeFlag.@decimal: { value = (T)(dynamic)((decimal)BitConverter.ToDouble(@byte, 0)); return(true); } case TargetTypeFlag.@timespan: { value = (T)(dynamic)TimeSpan.FromMilliseconds(BitConverter.ToDouble(@byte, 0)); return(true); } case TargetTypeFlag.@datetime: { value = (T)(dynamic) new DateTime(BitConverter.ToInt64(@byte, 0)); return(true); } } } catch { } return(false); }
/// <summary> /// 一些基元类型压缩 /// </summary> /// <param name="object"></param> /// <param name="value"></param> /// <param name="flag"></param> /// <returns></returns> protected bool TryCompressNullableTargetTypeFlag(object @object, TargetTypeFlag enumFlag, out byte[] value, out byte flag) { value = null; flag = (byte)enumFlag; switch (enumFlag) { case TargetTypeFlag.@char: { char?convert = (char?)@object; if (convert.HasValue == false) { return(false); } value = BitConverter.GetBytes(convert.Value); return(true); } case TargetTypeFlag.@bool: { bool?convert = (bool?)@object; if (convert.HasValue == false) { return(false); } value = BitConverter.GetBytes(convert.Value); return(true); } case TargetTypeFlag.@byte: { byte?convert = (byte?)@object; if (convert.HasValue == false) { return(false); } value = BitConverter.GetBytes(convert.Value); return(true); } case TargetTypeFlag.@short: { short?convert = (short?)@object; if (convert.HasValue == false) { return(false); } value = BitConverter.GetBytes(convert.Value); return(true); } case TargetTypeFlag.@ushort: { ushort?convert = (ushort?)@object; if (convert.HasValue == false) { return(false); } value = BitConverter.GetBytes(convert.Value); return(true); } case TargetTypeFlag.@int: { int?convert = (int?)@object; if (convert.HasValue == false) { return(false); } value = BitConverter.GetBytes(convert.Value); return(true); } case TargetTypeFlag.@uint: { uint?convert = (uint?)@object; if (convert.HasValue == false) { return(false); } value = BitConverter.GetBytes(convert.Value); return(true); } case TargetTypeFlag.@long: { long?convert = (long?)@object; if (convert.HasValue == false) { return(false); } value = BitConverter.GetBytes(convert.Value); return(true); } case TargetTypeFlag.@ulong: { ulong?convert = (ulong?)@object; if (convert.HasValue == false) { return(false); } value = BitConverter.GetBytes(convert.Value); return(true); } case TargetTypeFlag.@float: { float?convert = (float?)@object; if (convert.HasValue == false) { return(false); } value = BitConverter.GetBytes(convert.Value); return(true); } case TargetTypeFlag.@double: { double?convert = (double?)@object; if (convert.HasValue == false) { return(false); } value = BitConverter.GetBytes(convert.Value); return(true); } case TargetTypeFlag.@decimal: { decimal?convert = (decimal?)@object; if (convert.HasValue == false) { return(false); } value = BitConverter.GetBytes((double)convert.Value); return(true); } case TargetTypeFlag.@timespan: { TimeSpan?convert = (TimeSpan?)@object; if (convert.HasValue == false) { return(false); } value = BitConverter.GetBytes(convert.Value.TotalMilliseconds); return(true); } case TargetTypeFlag.@datetime: { DateTime?convert = (DateTime?)@object; if (convert.HasValue == false) { return(false); } value = BitConverter.GetBytes(convert.Value.Ticks); return(true); } } return(false); }
/// <summary> /// 一些基元类型压缩 /// </summary> /// <param name="object"></param> /// <param name="value"></param> /// <param name="flag"></param> /// <returns></returns> protected bool TryCompressTargetTypeFlag(object @object, TargetTypeFlag enumFlag, out byte[] value, out byte flag) { value = null; flag = (byte)enumFlag; switch (enumFlag) { case TargetTypeFlag.@char: { char convert = (char)@object; value = BitConverter.GetBytes(convert); return(true); } case TargetTypeFlag.@bool: { bool convert = (bool)@object; value = BitConverter.GetBytes(convert); return(true); } case TargetTypeFlag.@byte: { byte convert = (byte)@object; value = BitConverter.GetBytes(convert); return(true); } case TargetTypeFlag.@short: { short convert = (short)@object; value = BitConverter.GetBytes(convert); return(true); } case TargetTypeFlag.@ushort: { ushort convert = (ushort)@object; value = BitConverter.GetBytes(convert); return(true); } case TargetTypeFlag.@int: { int convert = (int)@object; value = BitConverter.GetBytes(convert); return(true); } case TargetTypeFlag.@uint: { uint convert = (uint)@object; value = BitConverter.GetBytes(convert); return(true); } case TargetTypeFlag.@long: { long convert = (long)@object; value = BitConverter.GetBytes(convert); return(true); } case TargetTypeFlag.@ulong: { ulong convert = (ulong)@object; value = BitConverter.GetBytes(convert); return(true); } case TargetTypeFlag.@float: { float convert = (float)@object; value = BitConverter.GetBytes(convert); return(true); } case TargetTypeFlag.@double: { double convert = (double)@object; value = BitConverter.GetBytes(convert); return(true); } case TargetTypeFlag.@decimal: { decimal convert = (decimal)@object; value = BitConverter.GetBytes((double)convert); return(true); } case TargetTypeFlag.@timespan: { TimeSpan convert = (TimeSpan)@object; value = BitConverter.GetBytes(convert.TotalMilliseconds); return(true); } case TargetTypeFlag.@datetime: { DateTime convert = (DateTime)@object; value = BitConverter.GetBytes(convert.Ticks); return(true); } } return(false); }