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

  • SEARCH
  • Home
  • 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 6756225
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T13:29:57+00:00 2026-05-26T13:29:57+00:00

I deserialized json string to List<ClassB> and now I want to cast it to

  • 0

I deserialized json string to List<ClassB> and now I want to cast it to List<ClassA> before I return it from
BindModel method. I need casting because the methods expects to get List<ClassA>.

Why I get error while casting? After all, ClassB inherits from ClassA. What should I do?

P.S. this question is extended from this post. In line new DataContractJsonSerializer(typeof(List<ClassB>)); instead of List<ClassB> the type will be constructed at runtime.

    public override object BindModel(...)
    {
          var serializer = new DataContractJsonSerializer(typeof(List<ClassB>));
          MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes("[{\"id\":\"1\",\"name\":\"name\"}]"));
          var list = serializer.ReadObject(ms);

          return (List<ClassA>)list;
    }

    [KnownType(typeof(ClassA))]
    [DataContract]
    public class ClassA
    {
        public ClassA();
    }

    [KnownType(typeof(ClassB))]       
    [DataContract]
    public class ClassB : ClassA
    {
        [DataMember(Name = "id")]
        public int Id { get; set; }
        [DataMember(Name = "name")]
        public string CategoryName { get; set; }
    }
  • 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-05-26T13:29:58+00:00Added an answer on May 26, 2026 at 1:29 pm

    You can use

     Cast<T>()
    

    For example:

     List<A> listOfA = new List<B>().Cast<A>();
    

    This in fact is inferior to Linq and is implemented on IEnumerable rather than IEnumerable<T> but still is useful. It is not efficient since as I said, it tries to cast it.

    Remember List does not allow for covariance which is a nuisance. It is preferable to use IEnumerable<T> as interface rather than List.

    You can say:

     IEnumerable<B> listOfB = new List<B>();
     IEnumerable<A> listOfA = listOfB; // no casting required
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to filter a generic List with data from a Json object. I
I have a exception from lift-json when I want to deserialize a json string.
I have the following C# helper class which retrieves a Json String from a
I have a JSON string such as below. That comes from a Website (the
Data sent from client will not get deserialized. Client: $.ajax({ type: 'POST', dataType: json,
I have a problem Deserialising a JSON string to a List the TCProject is
I've got a JSON string that I want to convert to a Map structure
I have a simple object that is deserialized from JSON into a server-side object.
I'm receiving a Json string back from a http request that looks something like
I have a JSON multi-level response that I need to deserialize and from the

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.