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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T01:48:37+00:00 2026-06-11T01:48:37+00:00

Can I avoid adding additional fields to a class to store data which is

  • 0

Can I avoid adding additional fields to a class to store data which is needed only for deserialization/serialization?

Suppose I have some class:

[Serializable]
class MyClass {
      [NonSerialized]
      NonSerializableDataType myField;

      SomeOtherDataType serializableTemporaryData;

      [OnSerializing]
      OnSerializing (StreamingContext context) {
          // build serializableTemporaryData from myField
      }

      [OnDeserialized]
      void OnDeserialized (StreamingContext context) {
         // build myField from serializableTemporaryData
      }
}

Is there any way to avoid having the serializableTemporaryData field in each object of MyClass? Can I, for example, make it static (possibly by changing my On… methods)?

Constraint: I cannot change the implementation of NonSerializableDataType.

Example: Suppose myField contains a handle to a resource. Then, on serialization, I must store some information on how to acquire the resource after de-serialization, but I cannot store the handle itself. If I wrap the handle into another class, then I have just shifted the problem to the wrapper class — I would ask the very same question for the wrapper class then.

  • 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-11T01:48:39+00:00Added an answer on June 11, 2026 at 1:48 am

    If you need to control the serialization process, you should implement the ISerialization interface.

    [Serializable]
    public class MyClass: ISerializable
    {
        // As you are in control of serialization process now
        // [Serialized] and [NonSerialized] attributes are no longer required
        private NonSerializableDataType myField;
    
        public virtual void GetObjectData(SerializationInfo info, StreamingContext context)
        {
            // Create and populate your SomeOtherDataType local variable here, then push it into info variable
            // Or even better, dont create SomeOtherDataType, just put additional serialization data into info variable, for example:
            info.AddValue("URL", "http://this.way.com");
        }
    
        protected MyClass(SerializationInfo info, StreamingContext context)
        {
            // Dont forget to define constructor for deserialization purpose
    
            this.myField = new NonSerializableDataType(loadFromURL: (string)info.GetValue("URL", typeof(string)));
        }
    }
    

    No additional class just for serialization data, no fields pollution. The only potential problem is to watch for any serializable data derived from this class (overwrite GetObjectData(...) if needed).

    More info: MSDN ISerializable

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

Sidebar

Related Questions

I link with Qt statically, so can linker or some other tool avoid adding
How can I avoid NullPointerExceptions ? I have tried using try-catch blocks but that
I have created several WCF services. In order to avoid adding service references in
I've come to the point where I need to store some additional data about
I know I can avoid boxing by adding my own Equals implementation. public struct
I know in Jenkins you can avoid duplicate builds from running on top of
Is there any way I can avoid using array_flip to optimize performance. I am
I read of a useful trick about how you can avoid using the wrong
Can I avoid the open DataReader exception (There is already an open DataReader associated
Can I avoid instantiate a Db object inside of Names object to access it

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.