1

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.

6
  • 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. Commented Feb 27, 2018 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#. Commented Feb 27, 2018 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. Commented Feb 27, 2018 at 11:21
  • 1
    @BarrJ i didn't see your answer and I did't down voted your answer. Commented Feb 27, 2018 at 11:28
  • 1
    @BarrJ You appear to have confused a C# enum with a property that happens to be called Enum. Commented Feb 27, 2018 at 11:43

1 Answer 1

4

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.