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;
}
}
精彩评论