c# - standard - phloe autofixture

Ploeh AutoFixture was unable to create an instance from System.Runtime.Serialization.ExtensionDataObject (2)

I hope someone will find it useful, I've managed to get it to work with the PropertyTypeOmitter class as per the this thread:

public void Test()
    var fixture = new Fixture();
        new PropertyTypeOmitter(

    var person = fixture.CreateAnonymous<Person>();

internal class PropertyTypeOmitter : ISpecimenBuilder
    private readonly Type type;

    internal PropertyTypeOmitter(Type type)
        if (type == null)
            throw new ArgumentNullException("type");

        this.type = type;

    internal Type Type
        get { return this.type; }

    public object Create(object request, ISpecimenContext context)
        var propInfo = request as PropertyInfo;
        if (propInfo != null && propInfo.PropertyType == type)
            return new OmitSpecimen();

        return new NoSpecimen();

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.ComponentModel.INotifyPropertyChanged {

    private System.Runtime.Serialization.ExtensionDataObject extensionDataField;

    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>();


Ploeh.AutoFixture.ObjectCreationException: Ploeh.AutoFixture.ObjectCreationException: AutoFixture was unable to create an instance from System.Runtime.Serialization.ExtensionDataObject, most likely because it has no public constructor, is an abstract or non-public type..

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.

The easiest way to do it is:

fixture.Register<ExtensionDataObject>(() => 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.