C # common method expansion and packaging records

Time:2021-12-2

1. Object to byte [] data

/// 
///Convert object to byte number
/// 
///Need to convert objects
/// 
public static byte[] ConvertToBytes(this object obj)
{
    using (var stream = new MemoryStream())
    {
        var formatter=new BinaryFormatter();
        formatter.Serialize(stream,obj);
        return stream.GetBuffer();
    }
}

2. Object to stream type

/// 
///Object to data flow
/// 
///Objects to convert
/// 
public static MemoryStream ConvertToStream(this object obj)
{
    using (var stream = new MemoryStream())
    {
        var formatter = new BinaryFormatter();
        formatter.Serialize(stream, obj);
        return stream;
    }
}