0

I know that you can't serialize/deserialize using an interface but I'm confused by behaviour I'm seeing.

When I deserialize and cast back to the interface, some properties are null. But if I cast back to the concrete type the same property has a value?

So, given this XML (shortened for brevity):

<Page>
  <ComponentPresentations>
    <ComponentPresentation>
      <Component>
        <Categories>
          <Category>
            <Id>tcm:35-540-512</Id>

Deserializing with

var serializer = new XmlSerializer(typeof(Page));
page = (IPage)serializer.Deserialize(reader);

page.ComponentPresentations[0].Component.Categories <-- is null

But if I cast back to the type,

var serializer = new XmlSerializer(typeof(Page));
page = (Page)serializer.Deserialize(reader);

page.ComponentPresentations[0].Component.Categories <-- is not null!

The Page type exposes the interface Categories property and a non-interface property - I assume to get around the serializing interface problem.

public List<Category> Categories { get; set; }
[XmlIgnore]
IList<ICategory> IComponent.Categories
{
    get { return Categories as IList<ICategory>; }
}

Is this because the interface property doesn't expose a setter?

Aliostad
  • 80,612
  • 21
  • 160
  • 208
Neil
  • 2,688
  • 1
  • 23
  • 32

1 Answers1

1

No. The problem is Contravariance not being supported by List<T> and IList<T>. Here is a good reference.


Have a look at this simple code:

public interface IMyInterface
{

}

public class MyImplementation : IMyInterface
{

}

List<MyImplementation> myImplementations = new List<MyImplementation>();
Console.WriteLine(myImplementations as IList<IMyInterface> == null); // OUTPUT: true!!

So as you can see, Categories as IList<ICategory> will always be null. While Categories as IList<Category> will be OK.

Nothing to do with serialisation.

Aliostad
  • 80,612
  • 21
  • 160
  • 208