using Newtonsoft.Json;
|
using System;
|
using System.Collections.Generic;
|
using System.Collections.ObjectModel;
|
using System.Drawing;
|
using System.IO;
|
using System.Linq;
|
using System.Reflection;
|
using System.Runtime.Serialization.Formatters.Binary;
|
|
namespace Bro.Common.Helper
|
{
|
public static class StaticHelper
|
{
|
/// <summary>
|
/// 数值转换为byte数组 高位在前,低位在后
|
/// </summary>
|
/// <param name="number"></param>
|
/// <param name="size"></param>
|
/// <returns></returns>
|
public static byte[] IntToBytes(this int number, int size = 2)
|
{
|
byte[] result = new byte[size];
|
|
int temp = size;
|
while (temp > 0)
|
{
|
result[size - temp] = (byte)(number >> ((temp - 1) * 8) & 0xff);
|
|
temp--;
|
}
|
|
return result;
|
}
|
|
/// <summary>
|
/// 字节数组转换为整数
|
/// </summary>
|
/// <param name="data">字节数组</param>
|
/// <param name="HtL">true:数组序号低的在高位 false:数组序号低的在低位</param>
|
/// <returns></returns>
|
public static int BytesToInt(this byte[] data, bool HtL = true)
|
{
|
int res = 0;
|
|
for (int i = 0; i < data.Length; i++)
|
{
|
int index = i;
|
|
if (HtL)
|
{
|
index = data.Length - 1 - i;
|
}
|
|
res += data[index] << (8 * i);
|
}
|
|
return res;
|
}
|
|
/// <summary>
|
/// 将32位整形拆分为无符号16位整形
|
/// </summary>
|
/// <param name="num">需要拆分的32位整形</param>
|
/// <param name="bitNum">拆分为16位整形的位数 1或者2</param>
|
/// <param name="HtL">true:高位在前,低位在后;false:高位在后,低位在前</param>
|
/// <returns></returns>
|
public static List<ushort> ParseIntToUnsignShortList(this int num, int bitNum = 2, bool HtL = false)
|
{
|
if (bitNum == 2)
|
{
|
ushort high = (ushort)(num >> 16);
|
ushort low = (ushort)num;
|
|
if (HtL)
|
{
|
return new List<ushort>() { high, low };
|
}
|
else
|
{
|
return new List<ushort>() { low, high };
|
}
|
}
|
else
|
{
|
if (num < 0)
|
{
|
num = ushort.MaxValue + 1 + num;
|
}
|
|
return new List<ushort>() { (ushort)num };
|
}
|
}
|
|
/// <summary>
|
/// 将32位整形数组拆分为无符号16位整形数组
|
/// </summary>
|
/// <param name="list">需要拆分的32位整形</param>
|
/// <param name="bitNum">拆分为16位整形的位数 1或者2</param>
|
/// <param name="HtL">true:高位在前,低位在后;false:高位在后,低位在前</param>
|
/// <returns></returns>
|
public static List<ushort> ParseIntToUnsignShortList(this List<int> list, int bitNum = 2, bool HtL = false)
|
{
|
return list.SelectMany(u => u.ParseIntToUnsignShortList(bitNum, HtL)).ToList();
|
}
|
|
/// <summary>
|
/// 将ushort的集合转换为16位带符号整形
|
/// </summary>
|
/// <param name="numList"></param>
|
/// <param name="bitNum">合并的位数 1或者2</param>
|
/// <param name="HtL">true:高位在前,低位在后;false:高位在后,低位在前</param>
|
/// <returns></returns>
|
public static List<int> ParseUnsignShortListToInt(this List<int> numList, int bitNum = 2, bool HtL = false)
|
{
|
if (bitNum == 1)
|
{
|
return numList.ConvertAll(n =>
|
{
|
int num = n;
|
if (num > short.MaxValue)
|
{
|
num = num - ushort.MaxValue - 1;
|
}
|
|
return num;
|
});
|
}
|
else
|
{
|
List<int> list = new List<int>();
|
for (int i = 0; i < numList.Count; i += 2)
|
{
|
int high = HtL ? numList[i] : numList[i + 1];
|
int low = HtL ? numList[i + 1] : numList[i];
|
list.Add((high << 16) | low);
|
}
|
|
return list;
|
}
|
}
|
|
public static string GetExceptionMessage(this Exception ex)
|
{
|
string msg = "异常信息:" + ex.Message;
|
if (ex.InnerException != null)
|
{
|
msg += ";\r\n内部异常信息:" + ex.InnerException.GetExceptionMessage();
|
}
|
|
msg += (";\r\nStackTrace:" + ex.StackTrace);
|
|
return msg;
|
}
|
|
public static T DeepSerializeClone<T>(this T t)
|
{
|
return JsonConvert.DeserializeObject<T>(JsonConvert.SerializeObject(t));
|
}
|
|
public static void DataFrom<T1, T2>(this T1 destT, T2 sourceT, List<string> exceptionProps = null) where T1 : class where T2 : class
|
{
|
if (sourceT == null)
|
{
|
destT = null;
|
return;
|
}
|
|
PropertyInfo[] propDest = destT.GetType().GetProperties();//.Where(p => !(p.GetMethod.IsVirtual && !p.GetMethod.IsFinal)).ToArray();
|
PropertyInfo[] propSource = sourceT.GetType().GetProperties();
|
|
Array.ForEach(propDest, prop =>
|
{
|
if (exceptionProps == null || !exceptionProps.Contains(prop.Name))
|
{
|
if (prop.CanWrite)
|
{
|
PropertyInfo propS = propSource.FirstOrDefault(p => p.Name == prop.Name);
|
if (propS != null && propS.CanRead)
|
{
|
prop.SetValue(destT, propS.GetValue(sourceT));
|
}
|
}
|
}
|
});
|
}
|
|
public static Bitmap BitmapSerializeCopy(this Bitmap map)
|
{
|
Bitmap image = null;
|
using (MemoryStream ms = new MemoryStream())
|
{
|
BinaryFormatter bf = new BinaryFormatter();
|
bf.Serialize(ms, map);
|
ms.Seek(0, SeekOrigin.Begin);
|
image = (Bitmap)bf.Deserialize(ms);
|
|
//ms.Close();
|
}
|
|
return image;
|
|
//Bitmap image = new Bitmap(map.Width, map.Height);
|
|
//int bitdepth_per_pixel = Bitmap.GetPixelFormatSize(map.PixelFormat) / 8;
|
//BitmapData source_bitmapdata = null;
|
//BitmapData destination_bitmapdata = null;
|
|
//try
|
//{
|
// source_bitmapdata = map.LockBits(new Rectangle(0, 0, map.Width, map.Height), ImageLockMode.ReadWrite, map.PixelFormat);
|
// destination_bitmapdata = image.LockBits(new Rectangle(0, 0, image.Width, image.Height), ImageLockMode.ReadWrite, image.PixelFormat);
|
|
// int source_bitmapdata_bitdepth_width = source_bitmapdata.Width * bitdepth_per_pixel;
|
// int source_bitmapdata_height = source_bitmapdata.Height;
|
// int source_bitmapdata_bitdepth_stride = source_bitmapdata.Stride;
|
|
// unsafe
|
// {
|
// byte* source_ptr = (byte*)source_bitmapdata.Scan0;
|
// byte* destination_ptr = (byte*)destination_bitmapdata.Scan0;
|
|
// int offset = source_bitmapdata_bitdepth_stride - source_bitmapdata_bitdepth_width;
|
|
// for (int i = 0; i < source_bitmapdata_height; i++)
|
// {
|
// for (int j = 0; j < source_bitmapdata_bitdepth_width; j++, source_ptr++, destination_ptr++)
|
// {
|
// *destination_ptr = *source_ptr;
|
// }
|
|
// source_ptr += offset;
|
// destination_ptr += offset;
|
// }
|
// }
|
|
// map.UnlockBits(source_bitmapdata);
|
// image.UnlockBits(destination_bitmapdata);
|
//}
|
//catch { }
|
|
//return image;
|
}
|
|
public static void Sort<T>(this ObservableCollection<T> collection) where T : IComparable<T>
|
{
|
List<T> sortedList = collection.OrderByDescending(x => x).ToList();//这里用降序
|
for (int i = 0; i < sortedList.Count(); i++)
|
{
|
collection.Move(collection.IndexOf(sortedList[i]), i);
|
}
|
}
|
|
// 布尔类型转换为整型
|
public static int ToInt(this object obj)
|
{
|
if (Convert.ToBoolean(obj) == true)
|
return 1;
|
else
|
return 0;
|
}
|
|
// 整型转换为布尔类型
|
public static bool ToBool(this object obj)
|
{
|
if (Convert.ToInt32(obj) == 1)
|
return true;
|
else
|
return false;
|
}
|
}
|
}
|