XmlSerialization and interfaces

邮差的信 提交于 2019-12-25 01:49:07

问题


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?


回答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.



来源:https://stackoverflow.com/questions/10109183/xmlserialization-and-interfaces

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!