using System;
|
using System.Collections.Generic;
|
using System.Linq;
|
using System.Reflection;
|
using System.Text;
|
using System.Threading.Tasks;
|
using WIDESEAWCS_Common.Attributes;
|
|
namespace WIDESEAWCS_Common.Helper
|
{
|
public static class ConvertHelper
|
{
|
public static bool[] ByteToBoolArray(this byte data)
|
{
|
bool[] result = new bool[8];
|
for (int i = 0; i < 8; i++)
|
{
|
result[i] = (data & (1 << i)) != 0;
|
}
|
return result;
|
}
|
|
public static T ByteToBoolObject<T>(this byte data)
|
{
|
bool[] boolArray = ByteToBoolArray(data);
|
|
//Array.Reverse(boolArray);
|
|
Type type = typeof(T);
|
object? obj = Activator.CreateInstance(type);
|
if (obj == null)
|
{
|
throw new Exception($"类型实例化错误");
|
}
|
T result = (T)obj;
|
|
PropertyInfo[] propertyInfos = type.GetProperties();
|
foreach (PropertyInfo propertyInfo in propertyInfos)
|
{
|
BoolIndexAttribute? boolIndexAttribute = propertyInfo.GetCustomAttribute<BoolIndexAttribute>();
|
if (boolIndexAttribute != null)
|
{
|
if (boolIndexAttribute.Index >= 0 && boolIndexAttribute.Index < 8)
|
{
|
propertyInfo.SetValue(result, boolArray[boolIndexAttribute.Index]);
|
}
|
}
|
}
|
|
return result;
|
}
|
}
|
}
|