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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T10:19:01+00:00 2026-06-09T10:19:01+00:00

In. Net you can mark a field as non serializable, and it will be

  • 0

In. Net you can mark a field as non serializable, and it will be skipped during serialization.

I’m looking for a simple method that will allow me to control in runtime whether a specific field should be serialized.

  • 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-09T10:19:04+00:00Added an answer on June 9, 2026 at 10:19 am

    You are referring to “mark a field as non serializable”, so I assume you are using BinaryFormatter and [NonSerialized]. If so, the only way to do conditional serialization is by implementing ISerializable and adding a similar constructor, and putting the logic in the GetObjectData implementation. This is tedious and error prone, though. I would suggest looking at protobuf-net, which has simpler conditional serialization, using the standard patterns used by TypeDescriptor and XmlSerializer, but is still binary output (more efficient than BinaryFormatter, actually). Specifically:

    [ProtoContract]
    public class SomeType {
        [ProtoMember(1)]
        public string Name {get;set;}
    
        private bool ShouldSerializeName() {
           // return true to serialize Name, false otherwise
        }
    }
    

    This ShouldSerialize* is a standard name-based convention – nothing specific to this serializer.

    Here’s the same via ISerializable:

    [Serializable]
    public class SomeType : ISerializable
    {
        public SomeType() { }
        public string Name { get; set; }
    
    
        void ISerializable.GetObjectData(
                 SerializationInfo info, StreamingContext context)
        {
            if (/* should serialize Name */) info.AddValue("Name", Name);
            //... all other fields
        }
        protected SomeType(SerializationInfo info, StreamingContext context)
        {
            foreach (SerializationEntry entry in info)
            {
                switch (entry.Name)
                {
                    case "Name": Name = (string)entry.Value; break;
                    //... all other fields
                }
            }
        }
    }
    

    Lots more to maintain; in particular, you have to take responsibility for all members when using ISerializable – however, if you just use protobuf-net you can handle each on a case-by-case basis.

    Actually, you can mix-and-match too, i.e. if you are stuck with using BinaryFormatter, you can still offload the work to protobuf-net, but it will change the format (so won’t be compatible with old data). For example:

    [Serializable, ProtoContract]
    public class SomeType : ISerializable
    {
        public SomeType() { }
        [ProtoMember(1)]
        public string Name { get; set; }
        private bool ShouldSerializeName() { /* condition */ }
    
        void ISerializable.GetObjectData(
            SerializationInfo info, StreamingContext context)
        {
            Serializer.Serialize(info, this); 
        }
        protected SomeType(SerializationInfo info, StreamingContext context)
        {
            Serializer.Merge(info, this);
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In .NET you can mark certain methods as obsolete so that developers are alerted
Can anyone recommend software or a .NET library that will check for bounced emails
I'm reading Dependency Injection in .NET by Mark Seemann and I can not for
Is there any limit that application state in asp.net can store ?? Am trying
How can I create or mark a file as hidden using .NET?
I have an asp.net form with bunch of sections that can be expanded/collapsed and
In Java you can mark a method as might throw an exception e.g. 'public
How can I properly receive these Array of Inputs on asp.net? <input type=hidden name='field[name][]'
how the mark and sweep phases of .NET GC can run concurrently with application
how the mark and sweep phases of .NET GC can run concurrently with application

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.