'C# Newtonsoft JSON deserialize into another (internal) property

Is it possible to tell the deserializer (via e.g. ContractResolver) to write the value of a Property directly to its internal or private backing field instead of using its setter?

Model:

    public class TestModel
    {
        public string Name
        {
            get => _Name; 
            set
            {
                // do some very expensive stuff
                _Name = value;
            }
        }

        internal string _Name = string.Empty;
    }

So my expectation is, that after some magic code the Deserializer writes the value for Property "Name" to the field "_Name" and avoid the expensive extra work done in setter of the Name prop.



Solution 1:[1]

Firstly, how do you actually know that there is a performance difference in both approaches? And how do you know that this is significant enough to justify such optimisation?

If you look at this

https://www.jacksondunstan.com/articles/2968

and this

Performance overhead for properties in .NET

Then you can see that you are really trying to over optimise this, and you are basically not going to gain any performance. You will certainly not gain anything that is significantly noticeable by the end user.

If you are so concerned about performance, then you shouldn't be using JSON!

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 jason.kaisersmith