5

I'm using JSON.NET to deserialize a JSON file to a dynamic object in C#.

Inside a method, I would like to pass in a string and refer to that specified attribute in the dynamic object.

For example:

public void Update(string Key, string Value)
    {
        File.Key = Value;
    }

Where File is the dynamic object, and Key is the string that gets passed in. Say I'd like to pass in the key "foo" and a value of "bar", I would do: Update("foo", "bar");, however due to the nature of the dynamic object type, this results in

{
 "Key":"bar"
}

As opposed to:

{
 "foo":"bar"
}

Is it possible to do what I'm asking here with the dynamic object?

3 Answers 3

4

I suspect you could use:

public void Update(string key, string Value)
{
    File[key] = Value;
}

That depends on how the dynamic object implements indexing, but if this is a Json.NET JObject or similar, I'd expect that to work. It's important to understand that it's not guaranteed to work for general dynamic expressions though.

If you only ever actually need this sort of operation (at least within the class) you might consider using JObject as the field type, and then just exposing it as dynamic when you need to.

3
  • You are indeed correct, I beat you to it by about 3 seconds :) Nov 19, 2015 at 7:08
  • 1
    @SeanMissingham: See the end of my answer for a suggestion about making this clearer though.
    – Jon Skeet
    Nov 19, 2015 at 7:09
  • Just saw that, thanks! Will mark this one as answer due to best explanation :) Nov 19, 2015 at 7:10
2

Okay so it turns out I'm special. Here's the answer for those that may stumble across this in future,

Turns out you can just use the key like an array index and it works perfectly. So:

File[Key] = Value; Works the way I need as opposed to File.Key = Value;

Thanks anyway!

1
  • 1
    That's really a notable difference between c# and JavaScript: In JavaScript file.Foo and file["Foo"] are aequivalent. C# has adopted this, but in an inconsistent way as it seems. See also this answer for a comparison of different declarations which will work (and which not).
    – Matt
    Nov 19, 2015 at 15:43
1

You can do it, if you're using JObject from JSON.NET. It does not work with an 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 would declare File in the example above as

dynamic File = new ExpandoObject();

you would get a runtime error:

CS0021 Cannot apply indexing with [] to an expression of type 'ExpandoObject'

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.