0

My issue concerns the syntax of object initialization syntax in C#, specifically the syntax for initialize a List Property.

JSchema of the Newtonsoft .NET Schema library provide a Property named Enum that is a IList<JToken> and i want use object initialization to initialize an istance of a such JSchema class. To initialize this property i have to use a list o JToken called enumStrings.

Unfortunately the field Enum is readonly because it provides only the get, as you can see from JSchema.Enum.

//The list containing the values i want to use for initialization
List<JToken> enumString = ...

var schema = new JSchema
{
    Type = JSchemaType.Object,
    Properties =
    {
        { "EnumLabel", new JSchema
        {
            Type = JSchemaType.String,
            Enum = { listaenum } //ERROR: it expects a JToken not a List<JToken>
        } }
    }
};

I can't use the following solution too, because Enum property is read only:

Properties =
    {
        { "EnumLabel", new JSchema
        {
            Type = JSchemaType.String,
            Enum = new List<JToken>(enumStrings) //ERROR: a property without setter or inaccessible setter cannot be assigned to
        } }
    }

Any suggestion to accomplish this? The values are only contained in enumStrings and always change so they can be hard-coded in the object initializer.

msalafia
  • 2,703
  • 5
  • 23
  • 34
  • 1
    `Enum` is a read-only property on `JSchema`, and `JSchema` only has a parameterless public constructor. I can't answer your question right now, but it's clear that you will have to search in a different direction because this construction method won't compile. – Zoran Horvat Feb 27 '18 at 11:11
  • Looking the documentation it seems there is no way to change Enum. I am wondering if there is a syntax for list property initialization involving collections instead of list initializer. I am not so expert in C#. – msalafia Feb 27 '18 at 11:15
  • Your second piece of code is the right list initialization syntax, given that you already have a collection. However, `Enum` cannot be changed on `JSchema` and I don't know the right way to initialize it. Maybe you could try with `JSchemaGenerator` class but it doesn't look good to me. – Zoran Horvat Feb 27 '18 at 11:21
  • Unbelievable. I don't understand why this property is read only. The funny thing is that with the first syntax it doesn't scolds me if i provide a list of values. Is there any sort of Destructuring for List to fit where a list initialization is required? – msalafia Feb 27 '18 at 11:26
  • 1
    @BarrJ i didn't see your answer and I did't down voted your answer. – msalafia Feb 27 '18 at 11:28
  • 1
    @BarrJ You appear to have confused [a C# `enum`](https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/enum) with [a property that happens to be called `Enum`](https://www.newtonsoft.com/jsonschema/help/html/P_Newtonsoft_Json_Schema_JSchema_Enum.htm). – Rawling Feb 27 '18 at 11:43

1 Answers1

3

Collection initializers call an Add method or extension method on the property value.

Try creating an extension method:

public static class CollectionInitializerExtensionMethods
{
    public static void Add(this IList<JToken> list, IList<JToken> toAdd)
    {
         foreach (var a in toAdd)
         {
             list.Add(a);
         }
    }
}

Failing that, just create the schema object, then find your property and call AddRange on it manually.

Rawling
  • 49,248
  • 7
  • 89
  • 127