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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T19:00:44+00:00 2026-05-26T19:00:44+00:00

I have a datacontact with many members that has a custom class I would

  • 0

I have a datacontact with many members that has a custom class

I would like to force a new instance if the property is null on deserialization.

is there a way to do that?

  • 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-26T19:00:44+00:00Added an answer on May 26, 2026 at 7:00 pm

    If your are using DataContract serialization then you can override its default behaviour using the OnDeserialized attribute.

    From MSDN: When applied to a method, specifies that the method is called during deserialization of an object in an object graph. The order of deserialization relative to other objects in the graph is non-deterministic.

    Here is my sample code:

    namespace MySpace
    {
    
      public class MyCustomClass
      {
        public string MyStrData { get; set; }
      }
    
      [DataContract]
      public class Data
      {
        [DataMember]
        public int mInt;
    
        [DataMember]
        public MyCustomClass MyCustonObj;
    
    
    
        [OnDeserialized]
        void OnDeserialized(StreamingContext c)
        {
          if (MyCustonObj == null)
          {
            MyCustonObj = new MyCustomClass();
            MyCustonObj.MyStrData = "Overridden in serialization";
          }
        }
    
        [OnDeserializing]
        void OnDeserializing(StreamingContext c)
        {
          if (MyCustonObj == null)
          {
            MyCustonObj = new MyCustomClass();
            MyCustonObj.MyStrData = "Overridden in  deserializing";
          }
        }
    
        [OnSerialized]
        void OnSerialized(StreamingContext c)
        {
           // if you wan to  do somehing when serialized here or just remove them
    
        }
    
        [OnSerializing]
        void OnSerializing(StreamingContext c)
        {
           // if you wan to  do somehing during serializing here or just remove them    
        }
      }
    
    
    }
    [ServiceContract]
    public interface IService
    {
      [OperationContract]
      Data Method(Data dd);
    }
    
    public class Service : IService
    {
      public Data Method(Data dd)
      {
        return dd;
      }
    }
    
    class Program
    {
      static void Main(string[] args)
      {
        string Url = "http://localhost:8000/";
        Binding binding = new BasicHttpBinding();
        ServiceHost host = new ServiceHost(typeof(Service));
        host.AddServiceEndpoint(typeof(IService), binding, Url);
        host.Open();
        ChannelFactory<IService> fac = new ChannelFactory<IService>(binding);
        fac.Open();
        IService proxy = fac.CreateChannel(new EndpointAddress(Url));
        Data d = new Data();
        d.mInt = 5;
        Console.WriteLine("Data before calling service " + d.mInt);
        Console.WriteLine("Data before calling service " + (d.MyCustonObj == null ? "null" : d.MyCustonObj.MyStrData));
        d = proxy.Method(d);
        fac.Close();
        host.Close();
        Console.WriteLine("Data after calling service " + d.mInt);
        Console.WriteLine("Data after calling service " + d.MyCustonObj.MyStrData);
        Console.ReadLine();
      }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a class that is marked with DataContract attributes and I would like
I have an object in SQL (A) that has a many to many relationships
I have a class that needs a property set inside a LINQ-to-SQL query. My
Have youI know that the problem has been many times discussed in the web.
I have a DataContract class that I have to fill by values from the
I have a class Team that holds a generic list: [DataContract(Name = TeamDTO, IsReference
I have a program that is accessing many similar tables using a linq to
I have a class with a couple of members I want to serialise to
I have created many class as my data class i.e: [DataContract] public class pCity
I have an XML like: <?xml version=1.0 encoding=utf-16?> <ExportProjectDetailsMessage xmlns:i=http://www.w3.org/2001/XMLSchema-instance xmlns=http://schemas.datacontract.org/2004/07/Project> <CPProjectId>7665699f-6772-424c-8b7b-405b9220a8e7</CPProjectId> </ExportProjectDetailsMessage> I'm

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.