实体
[Serializable]
public class Person
{
[BinarySerialization(0, 4)]
public int Id { get; set; }
[BinarySerialization(4, 16)]
public string Name { get; set; }
[BinarySerialization(20, 4)]
public int Age { get; set; }
[BinarySerialization(24, 8)]
public DateTime Birthday { get; set; }
public override string ToString() => $"Id: {Id}, Name: {Name}, Age: {Age}, Birthday: {Birthday}";
}
注解部分
[AttributeUsage(AttributeTargets.Property, AllowMultiple = false)]
public class BinarySerializationAttribute : Attribute
{
public int Offset { get; }
public int Length { get; }
public BinarySerializationAttribute(int offset, int length)
{
Offset = offset;
Length = length;
}
}
主要转换方法
public static class ByteArrayExtensions
{
public static T Deserialize<T>(this byte[] bytes) where T : new()
{
if (bytes == null) throw new ArgumentNullException(nameof(bytes));
T entity = new T();
foreach (PropertyInfo property in typeof(T).GetProperties())
{
BinarySerializationAttribute attribute = property.GetCustomAttribute<BinarySerializationAttribute>();
if (attribute != null)
{
int offset = attribute.Offset;
int length = attribute.Length;
byte[] values = bytes.Skip(offset).Take(length).ToArray();
if (property.PropertyType == typeof(Int32))
{
int value = BitConverter.ToInt32(values, 0);
property.SetValue(entity, value);
}
else if (property.PropertyType == typeof(String))
{
string value = Encoding.UTF8.GetString(values);
property.SetValue(entity, value);
}
else if (property.PropertyType == typeof(DateTime))
{
long value = BitConverter.ToInt64(values, 0);
property.SetValue(entity, new DateTime(value));
}
else if (property.PropertyType == typeof(Decimal))
{
int[] bits = new int[4];
for (int i = 0; i < bits.Length; i++)
{
byte[] data = new byte[4];
Array.Copy(values, i * 4, data, 0, 4);
bits[i] = BitConverter.ToInt32(data, 0);
}
property.SetValue(entity, new Decimal(bits));
}
else if (property.PropertyType == typeof(Byte))
{
property.SetValue(entity, values[0]);
}
else if (property.PropertyType == typeof(Double))
{
double value = BitConverter.ToDouble(values, 0);
property.SetValue(entity, value);
}
else if (property.PropertyType == typeof(Int16))
{
short value = BitConverter.ToInt16(values, 0);
property.SetValue(entity, value);
}
else if (property.PropertyType == typeof(Int64))
{
long value = BitConverter.ToInt64(values, 0);
property.SetValue(entity, value);
}
else if (property.PropertyType == typeof(Single))
{
float value = BitConverter.ToSingle(values, 0);
property.SetValue(entity, value);
}
else if (property.PropertyType == typeof(UInt16))
{
ushort value = BitConverter.ToUInt16(values, 0);
property.SetValue(entity, value);
}
else if (property.PropertyType == typeof(UInt32))
{
uint value = BitConverter.ToUInt32(values, 0);
property.SetValue(entity, value);
}
else if (property.PropertyType == typeof(UInt64))
{
ulong value = BitConverter.ToUInt64(values, 0);
property.SetValue(entity, value);
}
}
}
return entity;
}
}
使用就是
byte[] bytes = { 0x01, 0x00, 0x00, 0x00, 0xE8, 0xAF, 0xAD, 0xE5, 0x85, 0xB3, 0xE8, 0xAF, 0x95, 0xE5, 0x9D, 0x80, 0x5E, 0x5B, 0x4E, 0x61, 0x0, 0x0, 0x0, 0x0, 0x19, 0x0, 0x0, 0x0, 0x3, 0x5, 0x1, 0x1, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0 };
Person person = bytes.Deserialize<Person>();