将队列保存到文件,然后加载它并与原始进行比较

时间:2013-05-23 10:50:14

标签: c# serialization queue compare

我有一个问题是将队列保存到文件,然后将其加载到新队列中,然后将旧队列与新队列进行比较。

我有一个简单的TestMessageClass,现在我想序列化Queue并将其保存到名为test.bin的文件中。我认为工作正常,但是当我将Queue加载到另一个变量并进行比较时,它会给我以下错误:

Expected: equivalent to 

Grid.NodeTests.MessageSink_Tests.MessageSinkTests+TestMessageClass, 
Grid.NodeTests.MessageSink_Tests.MessageSinkTests+TestMessageClass, 
Grid.NodeTests.MessageSink_Tests.MessageSinkTests+TestMessageClass, 
Grid.NodeTests.MessageSink_Tests.MessageSinkTests+TestMessageClass, 
Grid.NodeTests.MessageSink_Tests.MessageSinkTests+TestMessageClass, 
Grid.NodeTests.MessageSink_Tests.MessageSinkTests+TestMessageClass, 
Grid.NodeTests.MessageSink_Tests.MessageSinkTests+TestMessageClass, 
Grid.NodeTests.MessageSink_Tests.MessageSinkTests+TestMessageClass, 
Grid.NodeTests.MessageSink_Tests.MessageSinkTests+TestMessageClass, 
Grid.NodeTests.MessageSink_Tests.MessageSinkTests+TestMessageClass


But was:


Grid.NodeTests.MessageSink_Tests.MessageSinkTests+TestMessageClass, 
Grid.NodeTests.MessageSink_Tests.MessageSinkTests+TestMessageClass, 
Grid.NodeTests.MessageSink_Tests.MessageSinkTests+TestMessageClass, 
Grid.NodeTests.MessageSink_Tests.MessageSinkTests+TestMessageClass, 
Grid.NodeTests.MessageSink_Tests.MessageSinkTests+TestMessageClass, 
Grid.NodeTests.MessageSink_Tests.MessageSinkTests+TestMessageClass, 
Grid.NodeTests.MessageSink_Tests.MessageSinkTests+TestMessageClass, 
Grid.NodeTests.MessageSink_Tests.MessageSinkTests+TestMessageClass, 
Grid.NodeTests.MessageSink_Tests.MessageSinkTests+TestMessageClass, 
Grid.NodeTests.MessageSink_Tests.MessageSinkTests+TestMessageClass

如果我使用普通字符串将其排队,一切正常,但如果我使用我的TestMessageClass则不再有效。

[Serializable()]
class TestMessageClass
{
    public enum MessageType
    { 
        Broadcast,
        Unicast
    }
}

internal interface ISerializationService
{
      void Serialize(Stream outputStream, object obj);
      T Deserialize<T>(Stream inputStream);
}

public void Serialize(Stream outputStream, Object obj)
{

    IFormatter serializer = new BinaryFormatter();
    serializer.Serialize(outputStream, obj);


}

public T Deserialize<T>(Stream inputStream)
{

    var result = new BinaryFormatter().Deserialize(inputStream);

    return (T)result;

}
[Test]
public void CompareQueue()
{
    Queue queue1 = new Queue();

    var message = new TestMessageClass();

    for (int i = 0; i < 100; i++)
        queue1.Enqueue(message);

    ISerializationService service = new SerializationService();

    FileStream stream = File.Open("Test.bin" , FileMode.Create , FileAccess.Write);

    service.Serialize(stream, queue1);
    stream.Close();

    FileStream stream2 = File.Open("Test.bin" , FileMode.Open ,FileAccess.Read);

    Queue queue2 = service.Deserialize<Queue>(stream2);
    stream2.Close();
    CollectionAssert.AreEquivalent(queue1,queue2);

}

1 个答案:

答案 0 :(得分:2)

最简单的解决方案是在TestMessageClass上实现Equals,以确定2个不同的实例是否相等。通常,您可以通过比较每个实例上的所有属性来实现该方法。

当你序列化然后反序列化它时,它会创建新实例,所以默认的Equals实现将返回false,因为它只是比较实例是否相同。