Object Initializer: "The object constructor ... has no argument or settable return property..."

Hi,
I have looked around the Googular response
(only really seen one: f# - Object initialization syntax - Stack Overflow ; )
and hit my head against a wall on this a couple of different times, but now it bugs me enough that i seek assistance (perhaps that i may hit my head smarter, not harder?).

C# lets you do this with Object Initializer syntax.

// Deserialize using altername names.
var inputOptions = new JsonSerializerOptions
{
    TypeInfoResolver = new DefaultJsonTypeInfoResolver
    {
        Modifiers = { JsonExtensions.UseAternameNames<Json3rdPartyAlternativeNameAttribute>() },
    },
};

Writing the F# equivalent following the above stackoverflow link has worked for me in the past with certain Properties, passing them as named arguments in F# (so long as they allow setting!)
But the documentation for Modifiers property calls it a ‘get’ only. Not even ‘init’.

So how does C# even let you do this?
Further (and more importantly for my case : ),
how can i set this Property in F# (when i try, i get the above error from the subject).

Thank you,
-Gerald

Hi Gerald,

I understand your frustration! Let’s break it down:

In C#, the Object Initializer syntax allows you to set properties during object creation without explicitly invoking a constructor. This is possible because the compiler processes the object initializer by first accessing the parameterless constructor and then processing the member initializations1.

However, in F#, the equivalent is done using object expressions. Unfortunately, if a property is marked as get only (read-only), you can’t set it directly.

type JsonSerializerOptions() =
    member val TypeInfoResolver = new DefaultJsonTypeInfoResolver() with get

let inputOptions = 
    { JsonSerializerOptions.TypeInfoResolver with
        Modifiers = [JsonExtensions.UseAternameNames<Json3rdPartyAlternativeNameAttribute>()]
    }

In this example, we’re using an object expression to create an instance of JsonSerializerOptions and then modifying the TypeInfoResolver property wellnow near me