Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PopulateObject: populating anonymous type instance #490

Closed
krisrok opened this issue Feb 23, 2015 · 3 comments
Closed

PopulateObject: populating anonymous type instance #490

krisrok opened this issue Feb 23, 2015 · 3 comments

Comments

@krisrok
Copy link

krisrok commented Feb 23, 2015

i just stumbled upon a use case where i want to fill a anonymous object with values coming from json:

var o = new { foo = "123", bar = "456" };
JsonConvert.PopulateObject(@"{'foo': 'abc'}", o);
Console.WriteLine(o.foo); // outputs "123" instead of "abc"

but it does not work as long as o is of anonymous type.

@JamesNK
Copy link
Owner

JamesNK commented Feb 23, 2015

Anonymous objects don't have setters.

@JamesNK JamesNK closed this as completed Feb 23, 2015
@krisrok
Copy link
Author

krisrok commented Feb 24, 2015

i see, didn't know too much about anonymous types. there would be a way by modifying their backing fields but i guess you wouldn't want that it in the lib.

@krisrok
Copy link
Author

krisrok commented Feb 24, 2015

in any case, here's my workaround: https://dotnetfiddle.net/S4GPil

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants