You can do this if using JObject from JSON.NET. It does not work with ExpandoObject .
Example:
void Main() { var j = new Newtonsoft.Json.Linq.JObject(); var key = "myKey"; var value = "Hello World!"; j[key] = value; Console.WriteLine(j["myKey"]); }
This simple example prints "Hello World!" as expected. Hence
var File = new Newtonsoft.Json.Linq.JObject(); public void Update(string key, string Value) { File[key] = Value; }
does what you expect. If you declare File in the above example as
dynamic File = new ExpandoObject();
you will get a runtime error:
CS0021 Cannot apply indexing with [] to an expression of type "ExpandoObject"
source share