本文介绍了Ploeh AutoFixture无法从System.Runtime.Serialization.ExtensionDataObject创建实例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们有一个MVC项目,其中引用了WCF服务。这些引用向每个DTO和响应对象添加了(ExtensionDataObject)ExtensionData 属性,现在 AutoFixture 无法创建这些实例的匿名实例。

We have an MVC project with references to WCF services. Those references added (ExtensionDataObject)ExtensionData property to every DTO and Response object and now AutoFixture fails to create anonymous instances of these types.

示例:

public partial class SearchResultsDto : object,
    System.Runtime.Serialization.IExtensibleDataObject,
    System.ComponentModel.INotifyPropertyChanged {

    [System.NonSerializedAttribute()]
    private System.Runtime.Serialization.ExtensionDataObject extensionDataField;

    [global::System.ComponentModel.BrowsableAttribute(false)]
    public System.Runtime.Serialization.ExtensionDataObject ExtensionData {
        get {
                return this.extensionDataField;
            }
        set {
                this.extensionDataField = value;
            }
        }
    }

代码:

_fixture = new Fixture().Customize(new AutoMoqCustomization());
var dto = _fixture.CreateAnonymous<SearchResultsDto>();

例外:

问题:是否有一种方法可以在内部注册该对象AutoFixture,以便将其实例化为 null 或其他任何值,这将允许我对具有以下内容的所有对象执行 CreateAnonymous ExtensionData 属性。

Question: Is there a way of registering this object within the AutoFixture, so that it instantiates it as null or anything else, which would let me do CreateAnonymous on all objects with ExtensionData property.

推荐答案

最简单的方法是:

fixture.Register<ExtensionDataObject>(() => null);

这会向AutoFixture注册一种特定的方式来初始化所有ExtensionDataObject(带有Func)。在这种情况下,Func总是返回null,所以您可以使用。

That registers to AutoFixture a specific way to initialize all the ExtensionDataObject, with the Func given. In this case the Func always returns null so you are good to go.

这篇关于Ploeh AutoFixture无法从System.Runtime.Serialization.ExtensionDataObject创建实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-24 14:59