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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T14:14:01+00:00 2026-05-12T14:14:01+00:00

I was reading custom serialization article on msdn: http://msdn.microsoft.com/en-us/library/ty01x675%28VS.80%29.aspx It mentions that there are

  • 0

I was reading custom serialization article on msdn: http://msdn.microsoft.com/en-us/library/ty01x675%28VS.80%29.aspx

It mentions that there are two ways of implementing custom serialization:
1, using OnDeserializedAttribute, OnDeserializingAttribute, OnSerializedAttribute, OnSerializingAttribute
2, implement ISerializable interface

According to MSDN, method #1 is “best practice and easiest”, but I fail to understand how are the two methods even the same thing. To my understanding, the OnSerializing|OnSerialized|etc attributes allow you to hook methods to particular phases of the serialization, while ISerializable interface allows you to directly modify what goes into and comes out of SerializationInfo. Is this correct?

To put my confusion into context, how do you implement OnSerializing to serialize object field under different name? like the following code:

public virtual void GetObjectData(SerializationInfo info, StreamingContext context)
{
    info.AddValue("i", n1);
    info.AddValue("j", n2);
    info.AddValue("k", str);
}

Update: Although Frederik Gheysels’s answer is not completely correct (decorating with Serializable is pre-requisite for both OnSerializing/OnDeserializing attributes and ISerializable interface), he did however points out that OnSerializing/OnDeserializing attributes are meant to be supplement of ISerializable, so I am accepting his answer on this point.

  • 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-12T14:14:02+00:00Added an answer on May 12, 2026 at 2:14 pm

    OnSerializing / OnDeserializing is complementary to ISerializable or SerializableAttribute.

    In other words: OnSerializing / OnDeserializing is not an alternative for ISerializable or the SerializableAttribute.

    These 2 attributes (OnSerializing / OnDeserializing) allow you to take control / perform extra actions when an object is serialized or deserialized. But, in order to be able to do this, the type to which these attributes are applied, has to be serializable (and thus implement the ISerializable interface, or decorated with the Serializable attribute).

    This is also said like this in the MSDN article you’re referring to:
    – OnSerializing and OnDeserializing are used to ‘correct’ data during and after serialization.

    You can test it quite easily.
    Create a type like this, and try to serialize it:

    public class Foo
    {
        private int _bar = 5;
    
        [OnSerializing]
        public void OnSerializeFoo( StreamingContext context )
        {
            _bar = 10;
        }
    }
    

    Note that the type does not implement ISerializable, nor is it decorated with the SerializableAttribute.
    Try to serialize an instance of that type, and you’ll be faced with a SerializationException, because Foo is not serializable.

    So, you can use these attributes (OnSerializing, OnDeserializing) to implement additional logic that should be performed when an instance of a certain type is serialized or deserialized.

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

Sidebar

Related Questions

After reading the MSDN article ( http://msdn.microsoft.com/en-us/magazine/2009.01.genevests.aspx ) on implementing a Custom STS using
Reading through an Article about custom Objects: http://technet.microsoft.com/en-us/library/ff730946.aspx I wonder why i get a
I'm reading this documentation article about Custom Annotations in Doctrine . I understand that
I was reading this tutorial. http://ben.onfabrik.com/posts/pagedown-markdown-editor-custom-image-dialog At the end of it there is ASP.NET
Reading MSDN (and other sources) about custom report items (CRI) for reporting services 2005.
I have custom model file format that I am reading from to create a
First i am reading this article .He uses a custom in memory repository for
I have a custom file type that I'm reading from and writing to within
I am trying create custom namespaces after reading the article at Custom Namespaces I
I was reading a while ago somewhere online that you could make a custom

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.