简体   繁体   中英

convert an object to a byte array with defined method in other class in C#

I have a struct which has some fields. I need a method to convert these fields to a byte array. There is a method, which I can use (" ObjectToByteArray"). I want to know how to use it. Is my code Correct??

public struct Convertor
{
    public string license { get; set; }
    public int Software { get; set; }
    public int StartDate { get; set; }
    public int EndDate { get; set; }
    public byte Count { get; set; }
    public int[] ActionCode { get; set; }

    public byte[] ConvertToArray()
    {
       var Result=new Convertor();

       return Result.ObjectToByteArray();
    }
}

Here is an example using System.Runtime.Serialization.Formatters.Binary.BinaryFormatter and the [Serializable()] attribute for the structure.

[Serializable()]
public struct Convertor 
{
    public string License { get; set; }
    public int Software { get; set; }
    public int StartDate { get; set; }
    public int EndDate { get; set; }
    public byte Count { get; set; }
    public int[] ActionCode { get; set; }

    public byte[] ConvertToArray()
    {
        var bf = new BinaryFormatter();
        using (var mem = new MemoryStream())
        {
            bf.Serialize(mem, this);
            return mem.ToArray();
        }
    }

    public static Convertor ConvertFromArray(byte[] buffer)
    {
        var bf = new BinaryFormatter();
        using (var mem = new MemoryStream(buffer))
        {
            return (Convertor)bf.Deserialize(mem);
        }
    }

    /// <summary>
    /// Checks for equality among <see cref="Convertor"/> classes
    /// </summary>
    /// <param name="other">The other <see cref="Convertor"/> to compare it to</param>
    /// <returns>True if equal</returns>
    public bool Equals(Convertor other)
    {
        return License == other.License
            && Software == other.Software
            && StartDate == other.StartDate
            && EndDate == other.EndDate
            && Count == other.Count
            && ActionCode.SequenceEqual(other.ActionCode);
    }

}
class Program
{
    static void Main(string[] args)
    {
        // Create a new object and add some data to it
        var a = new Convertor()
        {
            License = "ABC001",
            Software = 1,
            StartDate = 2018,
            EndDate = 2019,
            Count = 16,
            ActionCode = new[] { 67, 79, 68, 69, 49, 50, 51 }
        };

        // Serialize the object into a byte array
        var buffer = a.ConvertToArray();

        // Deserialize a new object from the byte array
        var b = Convertor.ConvertFromArray(buffer);

        // Check for equality
        var ok = a.Equals(b); // ok = true
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM