开发者

write struct data to binary file

I received some data (many times) which are encapsulated inside a开发者_运维问答 struct. what I need to do is write them to a file (binary) to restore the data. how will you do it?


Implement ISerializable (greater customization) or mark with the [Serializable] attribute (easier to use). Then use a BinaryFormatter to serialize to a file.


public struct MyStruct : ISerializable
    {
        #region ISerializable Members

        public void GetObjectData(SerializationInfo info, StreamingContext context)
        {
            throw new NotImplementedException();
        }

        #endregion

        public override int GetHashCode()
        {
            return base.GetHashCode();
        }

        public override bool Equals(object obj)
        {
            return base.Equals(obj);
        }

        public static bool operator ==(MyStruct m1, MyStruct m2)
        {
            return true;
        }

        public static bool operator !=(MyStruct m1, MyStruct m2)
        {
            return false;
        }
    }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜