Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7769941
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T16:17:02+00:00 2026-06-01T16:17:02+00:00

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

  • 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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T16:17:03+00:00Added an answer on June 1, 2026 at 4:17 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I know that we can protect .Net applications from decompiling by using obfuscators. But
I know that it's a subject that can raise a lot of debate, but
I know that this can be easily done by using if(i%5 == 0 OR
I know that I can use cmp, diff, etc to compare two files, but
I know that there can be multiple values for an email, but I'm not
I want to know that can we build a data object using jquery/json, which
I know that you can convert a float into a string using NSString *myString
I know that some website are applications, but not all websites are applications (albeit
I know that you can control an MMC snap-in using the MMC 2.0 Automation
I know that can co-exists with web form no problem as Hanselman mention here

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.