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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T00:40:48+00:00 2026-05-13T00:40:48+00:00

I have a bunch of stored, serialized classes (that all inherit from a base

  • 0

I have a bunch of stored, serialized classes (that all inherit from a base class) in C#. Along with the serialized class, I also store an enum value that identifies the which subclass is serialized.

This means that whenever I want to serialize/deserialize the class, I’ve got to use a couple of big switch statements based on the enum to decide which subclass to deserialize into. It seems like there would be a be a way to do it using some kind of structuremap-like thing. (Its for a widget/dashboard, so its completely possible that many more subclasses will appear in the future).

Right now, It looks like this…

 widget = DeserializeFromDb(GetWidgetType(widgetrow.WidgetType),  widgetRow.serializedWidget);

   private HtmlWidget DeserializeFromDb(WidgetType type, string serialized)
   {
        Basics.Serial.IStringSerializer serializer = Basics.Serial.BinarySerializer.GetInstance();

        switch (type)
        {
            case WidgetType.AbstractBase:
                return serializer.Deserialize<HtmlWidget>(serialized);
                break;
            case WidgetType.Widget1:
                return serializer.Deserialize<Widget1>(serialized);
                break;
            case WidgetType.Widget2:
                return serializer.Deserialize<Widget2>(serialized);
                break;
       }
  }
  • 1 1 Answer
  • 1 View
  • 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-13T00:40:48+00:00Added an answer on May 13, 2026 at 12:40 am

    You can map enum values to Func<ISerializer, string, HtmlWidget> delegates:

    static Dictionary<WidgetType, Func<ISerializer, string, HtmlWidget>> map = 
       new Dictionary<WidgetType, Func<ISerializer, string, HtmlWidget>> {
          { WidgetType.AbstractBase, (s, o) => s.Deserialize<HtmlWidget>(o) },
          { WidgetType.Widget1, (s, o) => s.Deserialize<Widget1>(o) }, 
       // ...
    };
    
    // use it like:
    return map[type](serializer, serialized);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a bunch of NSDictionaries thats stored in an NSArray. The dictionaries store
I have a bunch of images stored in my database as blobs that I
Currently I have a bunch of HTML stored in a variable that I am
I have a bunch of Word documents (.doc) stored in my SQL database that
Lets say we have a bunch of numbers that increment in small values from
I have a bunch of information from a few views (stored in a singleton)
Okay. I have a database that has bunch of stored procedures and references another
I have a bunch of Joda time Interval objects stored in a List. All
I have a bunch of posts stored in text files formatted in yaml/textile (from
I have a bunch of data from a scientific experiment stored in a MySQL

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.