【问题标题】:convert an object to a byte array with defined method in other class in C#使用 C# 中其他类中定义的方法将对象转换为字节数组
【发布时间】:2018-03-25 13:32:44
【问题描述】:

我有一个struct,它有一些字段。我需要一种将这些字段转换为byte 数组的方法。有一种方法,我可以使用(“ObjectToByteArray”)。 我想知道如何使用它。我的代码正确吗?

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();
    }
}

【问题讨论】:

  • 您正在寻找非常广泛的主题“序列化”。
  • @JPVenson - 特别是二进制序列化。除非您想手动为每个字段手动调用 BitConverter.GetBytes()
  • @ja72 当然 ;-)。仍然很广泛的问题。
  • @ja72 即使你这样做,它仍然是“序列化”......请原谅“二进制序列化”;-)
  • 你看到这个答案了吗:stackoverflow.com/a/10502856/3668866

标签: c#


【解决方案1】:

这是一个使用System.Runtime.Serialization.Formatters.Binary.BinaryFormatter[Serializable()] 属性的结构示例。

[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
    }
}

【讨论】:

    猜你喜欢
    • 2016-07-17
    • 2021-11-30
    • 2018-02-14
    • 1970-01-01
    • 2017-01-27
    • 1970-01-01
    • 2012-01-16
    • 2014-12-19
    • 1970-01-01
    相关资源
    最近更新 更多