有没有人创建过 DataContract 测试工具? [英] Has anyone created a DataContract testing tool?

查看:25
本文介绍了有没有人创建过 DataContract 测试工具?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有人见过测试 WCF DataContracts 的库?提出这个问题的动机是我刚刚在我的应用程序中发现了一个错误,该错误是由于我没有使用 DataMember 属性注释属性而导致的 - 结果,该属性没有被序列化.

Has anyone seen a library that tests WCF DataContracts? The motivation behind asking this is that I just found a bug in my app that resulted from my not annotating a property with the DataMember attribute - as a result, that property wasn't being serialized.

我想到的是一种 API,如果给定特定类型的 DataContract,它将自动使用随机数据填充其成员,包括任何子 DataContract,然后通过 WCF 序列化程序/格式化程序之一对其进行序列化,然后检查所有的数据已被转移.

What I have in mind is an API that, given a particular type of DataContract, will automatically populate its members with random data, including any child DataContracts, then serialize it via one of the WCF Serializers/Formatters and then check that all of the data has been carried across.

有人吗?

推荐答案

使用 DataContractSerializer 将您的对象序列化为 MemoryStream,然后将其反序列化为作为一个新实例存在.

It's simple enough to use DataContractSerializer to serialise your object to a MemoryStream, then deserialise it back into existence as a new instance.

这是一个演示这种往返序列化的类:

Here's a class that demonstrates this round-trip serialisation:

public static class WcfTestHelper
{
    /// <summary>
    /// Uses a <see cref="DataContractSerializer"/> to serialise the object into
    /// memory, then deserialise it again and return the result.  This is useful
    /// in tests to validate that your object is serialisable, and that it
    /// serialises correctly.
    /// </summary>
    public static T DataContractSerializationRoundTrip<T>(T obj)
    {
        return DataContractSerializationRoundTrip(obj, null);
    }

    /// <summary>
    /// Uses a <see cref="DataContractSerializer"/> to serialise the object into
    /// memory, then deserialise it again and return the result.  This is useful
    /// in tests to validate that your object is serialisable, and that it
    /// serialises correctly.
    /// </summary>
    public static T DataContractSerializationRoundTrip<T>(T obj, 
                    IEnumerable<Type> knownTypes)
    {
        var serializer = new DataContractSerializer(obj.GetType(), knownTypes);
        var memoryStream = new MemoryStream();
        serializer.WriteObject(memoryStream, obj);
        memoryStream.Position = 0;
        obj = (T)serializer.ReadObject(memoryStream);
        return obj;
    }
}

您负责的两项任务:

  • 首先用合理的数据填充实例.您可能会选择使用反射来设置属性或提供带有参数的构造函数,但我发现这种方法除了非常简单的类型外,不适用于任何其他类型.
  • 在往返反序列化后比较这两个实例.如果您有可靠的 Equals/GetHashCode 实现,那么这可能已经为您完成了.您可以再次尝试使用通用反射比较器,但这可能并不完全可靠.
  • Populating the instance in the first place with sensible data. You might choose to use reflection to set properties or supply a constructor with its arguments, but I've found this approach just won't work for anything other than incredibly simple types.
  • Comparing the two instances after you have round-trip de/serialised it. If you have a reliable Equals/GetHashCode implementation, then that might already be done for you. Again you might try using a generic reflective comparer, but this mightn't be completely reliable.

这篇关于有没有人创建过 DataContract 测试工具?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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