PrivateSetterContractResolver 2.1.0

.NET Core 2.0 .NET Standard 2.0 .NET Framework 3.5
dotnet add package PrivateSetterContractResolver --version 2.1.0
NuGet\Install-Package PrivateSetterContractResolver -Version 2.1.0
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="PrivateSetterContractResolver" Version="2.1.0" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add PrivateSetterContractResolver --version 2.1.0
#r "nuget: PrivateSetterContractResolver, 2.1.0"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install PrivateSetterContractResolver as a Cake Addin
#addin nuget:?package=PrivateSetterContractResolver&version=2.1.0

// Install PrivateSetterContractResolver as a Cake Tool
#tool nuget:?package=PrivateSetterContractResolver&version=2.1.0


This small library provides a JSON.Net contract resolver to (de-)serialize properties with private setters or getter-only auto properties on a model.

Install-Package PrivateSetterContractResolver

You can activate the contract resolver using the JsonSettings when (de-)serializing json:

using Newtonsoft.Json;
using Newtonsoft.Json.Serialization;

JsonSerializerSettings settings = new JsonSerializerSettings() {
    ContractResolver = new PrivateSetterContractResolver()

// Use constructor to instantiate the model object
ApiResult model = new ApiResult("Error message");

// Serialize using public getters of the properties
string serialized = JsonConvert.SerializeObject(model, settings);
// { "success": false, "errorMessage": "Error message" }

// Deserialize by using no constructor and directly setting the backing field of the getter-only auto properties
model = JsonConvert.DeserializeObject<Model>(serialized, settings);
// model has same state right after instantiation

This contract resolver will not invoke any constructor when deserializing. The combination of setting getter-only auto properties and not using any initialization allows for a very resticted model class for creating valid model instances at runtime without affecting the deserialization process of the receiver. An example would be:

public class ApiResult {
    /// <summary>
    /// Creates a successful result with no error message.
    /// </summary>
    public ApiResult() {
        // This default constructor will NOT be called on deserialization
        Success = true;

    /// <summary>
    /// Creates a failed result with <paramref name="errorMessage"/> as message.
    /// </summary>
    /// <param name="errorMessage">The error message</param>
    public ApiResult(string errorMessage) {
        Success = false;
        ErrorMessage = errorMessage;

    public bool Success { get; }
    public string ErrorMessage { get; }

This model implements a default constructor in which the model is set in a successful state. This parameterless constructor would be called by default and the Success-Property wouldn't be changed to it's real value anymore since it's a getter-only auto property. With the PrivateSetterContractResolver however no constructor will be called at all and the getter-only property will be set to the correct value.

Using uninitialized objects can be dangerous, so use this contract resolver only in places where initialization is generally not needed (like in serializing api models).

Product Versions
.NET net5.0 net5.0-windows net6.0 net6.0-android net6.0-ios net6.0-maccatalyst net6.0-macos net6.0-tvos net6.0-windows net7.0 net7.0-android net7.0-ios net7.0-maccatalyst net7.0-macos net7.0-tvos net7.0-windows
.NET Core netcoreapp2.0 netcoreapp2.1 netcoreapp2.2 netcoreapp3.0 netcoreapp3.1
.NET Standard netstandard2.0 netstandard2.1
.NET Framework net35 net40 net403 net45 net451 net452 net46 net461 net462 net463 net47 net471 net472 net48 net481
MonoAndroid monoandroid
MonoMac monomac
MonoTouch monotouch
Tizen tizen40 tizen60
Xamarin.iOS xamarinios
Xamarin.Mac xamarinmac
Xamarin.TVOS xamarintvos
Xamarin.WatchOS xamarinwatchos
Compatible target framework(s)
Additional computed target framework(s)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (3)

Showing the top 3 NuGet packages that depend on PrivateSetterContractResolver:

Package Downloads

This is a project that expose Entity Framework using a Base Repositiory.


Package Description


Package Description

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
2.1.0 130,467 12/5/2017
2.0.0 830 12/5/2017
1.0.1 855 10/4/2017
1.0.0 854 10/2/2017