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 8431155
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T05:46:47+00:00 2026-06-10T05:46:47+00:00

I have 2 types: BaseQuestion and Question . Question inherits some properties from BaseQuestion

  • 0

I have 2 types: BaseQuestion and Question. Question inherits some properties from BaseQuestion. Now I have created a Web API to make a BaseQuestion available. The Question datatype has additional properties that I do not want to make available. I have a method that retrieves a Question and my initial plan was to just implicitly upcast it to BaseQuestion. I thought it would lose all extra properties that I do not want to make accessible and I could return it.
Well, it doesn’t. This is what I do:

Question q = allQuestions[0];
BaseQuestion bq = q;
string type = bq.GetType().ToString();

The type of bq is still “Question”. I cannot access the BaseQuestion properties, but I can still see them in the debugger and they are in the JSON output that I send to the client.

Any ideas on how I can “force” bq to be of type BaseQuestion and not to have any properties that are defined in the subclass?

  • 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-10T05:46:48+00:00Added an answer on June 10, 2026 at 5:46 am

    Typecasting doesn’t change the nature of the object, it only changes your view of the object. When you typecast to the base type, you’re looking at the object through a filter that can only see the members defined on the base type, regardless of whatever else is defined in the actual object.

    When you return an object from a web service call, it is the actual object that will be serialized and sent back across the wire – all of its serializable members.

    One technique you could use to prevent members of the derived Question class from being returned to the API caller is to suppress serialization of the members declared in the Question class. There are several serialization subsystems in .NET, but if you’re using XmlSerialization you would decorate the members declared in the Question class with [XmlIgnore] attributes to prevent them from being serialized by XmlSerialization. If you’re using a different serialization subsystem you will need to figure out the analog of this in that system.

    Another possibility is to define interface contracts for your web API service(s). Your BaseQuestion and Question classes would implement one or more of those contract interfaces. I believe this will limit the serialization to only the properties defined in the contract interface, regardless of what the actual object is.

    If all else fails, the brute force solution is to construct an instance of your BaseQuestion in a temp var, copy the relevant properties from the actual Question object to the temp object, and return the temp object. This is rude and crude and there’s got to be a better way, but it will work.

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

Sidebar

Related Questions

I have some types which are generated by a web service reference. I want
I have some types with a sequence inside, which does restrict the order of
I have two types of users on the database: -Web registered users -Twitter users
I have three types of questions: Vocab (represented by question:answer pairs) Grammar (represented by
I have some types that extend a common type, and these are my models.
I have some types that are structs for performance reasons and have some commonality.
I have 5 types of objects: place info (14 properties),owner company info (5 properties),
I have few types that derive from simplified Base as shown below. I am
I have some types that I want to serialize/deserialize and generate a UI based
I have many types of file in my eclipse workspace, so i want some

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.