如何单元测试我的对象是否真的可序列化? [英] How to unit test if my object is really serializable?

查看:20
本文介绍了如何单元测试我的对象是否真的可序列化?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 Nunit 测试中使用 C# 2.0.我有一些需要序列化的对象.这些对象相当复杂(不同层次的继承,包含大量的对象、事件和委托).

I am using C# 2.0 with Nunit Test. I have some object that needs to be serialized. These objects are quite complex (inheritance at different levels and contains a lot of objects, events and delegates).

如何创建单元测试以确保我的对象可以安全地序列化?

How can I create a Unit Test to be sure that my object is safely serializable?

推荐答案

我在工作中的一些单元测试中有这个:

I have this in some unit test here at job:

MyComplexObject dto = new MyComplexObject();
MemoryStream mem = new MemoryStream();
BinaryFormatter b = new BinaryFormatter();
try
{
    b.Serialize(mem, dto);
}
catch (Exception ex)
{
    Assert.Fail(ex.Message);
}

可能会帮助你...也许其他方法会更好,但这个方法效果很好.

Might help you... maybe other method can be better but this one works well.

这篇关于如何单元测试我的对象是否真的可序列化?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆