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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T15:53:43+00:00 2026-06-08T15:53:43+00:00

I am trying to serialize a type like follows: public UsersPanel(UsersVM userVm) { var

  • 0

I am trying to serialize a type like follows:

    public UsersPanel(UsersVM userVm)
    {
        var serialized = Serialize(userVm);
    }

    public static string Serialize(ViewModelBase instance)
    {
        var formatter = new BinaryFormatter();
        using (var stream = new MemoryStream())
        {
            formatter.Serialize(stream, instance); // breaks here
            return Convert.ToBase64String(stream.ToArray());
        }
    }

Where UsersVM is defined as

public class UsersVm : ViewModelBase {}

and ViewModelBase is defined as

[Serializable]
public class ViewModelBase {}

This is giving me the following error:

Type ‘UsersVM’ is not marked as serializable.

Why is it telling me this, if I’ve cast the object userVm to ViewModelBase (which is marked as Serializable) by passing it into Serialize(ViewModelBase instance)?

I would have thought that passing UsersVM would be replaced by the base type ViewModelBase when passing it into the method which takes ViewModelBase.

How can I serialize ViewModelBase?

  • 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-08T15:53:44+00:00Added an answer on June 8, 2026 at 3:53 pm

    Solving the Issue

    You have to mark your derived class as serializable, too

    [Serializable]
    public class UsersVm : ViewModelBase {}
    

    Why you have to do this

    The BinaryFormatter looks at the actual type of the object instance when serializing. The cast just tells the compiler to treat the instance as if it were a different type but does not actually change the instance into that type.

    Side Note

    I initially read the question backwards and found the answer to the inverse question interesting and potentially useful to others…

    Note if the situation were reversed (the base class were not marked serializable and you didn’t have access to the source code), you could still accomplish your goal.

    the subclass can implement ISerializable, use reflection to read and serialize the base classes’ fields, and use reflection again to set these fields during deserialization

    http://msdn.microsoft.com/en-us/magazine/cc163902.aspx#S14

    The article provides a code sample including a utility to help implement this approach.

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

Sidebar

Related Questions

Trying to serialize a union-like data-type. There is an enum field indicating the type
I am trying to serialize a PagedList object ( https://github.com/martijnboland/MvcPaging/blob/master/src/MvcPaging/PagedList.cs ) to Json, like
I've been trying to serialize an XML (jQuery object) to string to POST it
I'm trying to serialize Dictionary<int, int> as Dictionary<string, string> . So I've created Test2
I'm trying to output a xml file without xml head like I tried: Type
I'm getting this error while trying to de-serialize my object : [org.xml.sax.SAXParseException: cvc-complex-type.2.4.b: The
I'm trying to build an object that looks something like this: public class MyObject
I'm trying to deserialize a relative simple JSON string, which looks like: [{ FacebookID:
I trying to serialize a custom type which holds a dictionary among other members.
I have a bean with CommonsMultipartFile type field like so: public class Placement implements

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.