C# Byte[] 和 T 互转

来源:互联网 发布:局域网添加网络打印机 编辑:程序博客网 时间:2024/06/05 08:27
        private T BytesToT<T>(byte[] bytes)        {            using (var ms = new MemoryStream())            {                ms.Write(bytes, 0, bytes.Length);                var bf = new BinaryFormatter();                ms.Position = 0;                var x = bf.Deserialize(ms);                return (T)x;            }        }        private byte[] TToBytes<T>(T obj)        {            var bf = new BinaryFormatter();            using (var ms = new MemoryStream())            {                bf.Serialize(ms, obj);                return ms.ToArray();            }        }

1 0
原创粉丝点击