How should you type an enumerable property in .NET Core configuration when using options?

183 Views Asked by At

I'm trying to work out the best way of using enumerable properties in an options object that's populated through appsettings.json configuration.

I have an example object below that I'll be configuring from an appsettings.json file that looks like this:

public class MyOptions
{
    public ICollection<string> ListOne { get; set; } = Array.Empty<string>();

    public string[] ListTwo { get; set; } = Array.Empty<string>();

    public ICollection<string> ListThree { get; } = Array.Empty<string>();
}
{
    "MyOptions": {
        "ListOne": [ "one", "two", "three" ],
        "ListTwo": [ "one", "two", "three" ],
        "ListThree": [ "one", "two", "three" ]
    }
}

The problem here is that when using FxCop analysers a warning is raised against ListOne: CA2227: Collection properties should be read only. And a different one against ListTwo: CA1819: Properties should not return arrays. And if you the setter from ListOne like done in ListThree to fix the warning then the configuration isn't read in at all because it needs a setter.

So my question is really, well if you can't any of that, how do you populate an enumerable property without any warnings?

0

There are 0 best solutions below