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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T06:37:55+00:00 2026-05-18T06:37:55+00:00

I have a base class, SpecialClass . Lots of other classes inherit from it,

  • 0

I have a base class, SpecialClass. Lots of other classes inherit from it, like WriteSpecialClass and ReadSpecialClass. Instances of these classes are serialized and deserialized, after being casted to/from the base class. Therefore, I have lots of repetitious code like this (for serializing):

SpecialClass sc = null;

if (type.DisplayName == "Read") {
    sc = new ReadSpecialClass();
} else if (type.DisplayName == "Write") {
    sc = new WriteSpecialClass();
} else
    throw new NotSupportedException("Type not supported");

String xml = SerializeToXML(sc);

.. and deserializing:

SpecialClass sc = null;

sc = (SpecialClass)DeserializeFromXML(xml);

switch (someVariableThatDicatesTypeOfSpecialClass) {
    case "Read":
        sc = (ReadSpecialClass)sc;
        break;
    case "Write":
        sc = (WriteSpecialClass)sc;
        break;
}

One more important piece of info: SpecialClass has a bunch of abstract methods defined which each class that inherits from it needs to implement. All the classes which inherit from it conform to the methods, but return different things, so each class is not the same.

I can post my serialization or deserialization methods if needed.

I would like to try and simplify this so that I don’t have to specify each SpecialClass-derived class (like ReadSpecialClass). Is there any way to do this? The only way I can think of is duck-typing. Thanks for your help,

  • 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-18T06:37:55+00:00Added an answer on May 18, 2026 at 6:37 am

    For serializing, you can use some sort of lookup:

    public class SpecialClass
    {
        private static Dictionary<string, Func<SpecialClass>> factories =
            new Dictionary<string, Func<SpecialClass>>();
    
        static SpecialClass()
        {
            factories["Read"] = () => new ReadSpecialClass();
            factories["Write"] = () => new WriteSpecialClass();
        }
    
        public static SpecialClass CreateByName(string name)
        {
            Func<SpecialClass> factory;
    
            if (!factories.TryGetValue(name))
                throw new ArgumentException("name", "\"" name +
                    "\" is not a recognized subclass of SpecialClass.");
    
            return factory();
        }
    }
    

    For deserialization, these two lines:

    sc = (ReadSpecialClass)sc;
    sc = (WriteSpecialClass)sc;
    

    perform no actual conversion. The only thing they will do is throw an exception if the object referenced by sc is not of the appropriate type. What you are doing here is roughly the same thing as:

    object a = "foo";
    a = (string)a;
    

    Sure, the cast will succeed. But it does not in any way modify the object pointed to by a. All it really does is verify that this object is already a string.

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

Sidebar

Related Questions

I have a base class vehicle and some children classes like car, motorbike etc..
I have a base controller class from which my other controllers are inherited public
I have a base class A and classes B and C are derived from
I have these classes: class Base { public: virtual void foo(int x = 0)
I have a base class and several concrete classes that derive from it. Lets
I have base class BaseClass and derived classes DerivedA , DerivedB , and DerivedC
I have base class Class1 and a derived class2. I create two instances of
I have base-class Base from which is derived Derived1 , Derived2 and Derived3 .
I have a base class object array into which I have typecasted many different
I have a base class with an optional virtual function class Base { virtual

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.