1
Vote

Objects serialized with collections that refer to themselves fails with an exception

description

In ObjectFactory, there's this check:
        if (property.Type == null)
        {
            // there is no property type and no expected type defined. Give up!
            throw new InvalidOperationException(string.Format("Property type is not defined. Property: \"{0}\"",
                                                              property.Name));
        }
However, if you have a situation where you have an object that has a collection that has an object that has a reference to the first object, property.Type will be null, because it's a reference object.

The solution? Remove that check and serialization works fine. This affects both binary and xml serialization. The following example is a shortened version that shows the situation. I've trimmed it for brevity and removed identifying information. Obviously, this object graph is huge, so this is "fun":

???<Complex name="Root" type="MyNamespace.Model.User, MyNamespace.Model, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" id="1">
<Properties>
...
    <Complex name="Contact" id="3">
        <Properties>
        ...
            <Complex name="PreferredLocale" id="12">
                <Properties>
                ...
                    <Collection name="Contacts1" type="System.Collections.Generic.HashSet`1[[MyNamespace.Model.Contact, MyNamespace.Model, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]], System.Core, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
                        <Items>
                            <Reference id="3" />
                        </Items>
                    </Collection>


comments