Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

`Dynamic` to a static class converter. Does it exist?

I am using the dynamic type to read in a lot of JSON and process it. Frankly, I'd rather have intellisense. So I was wondering whether anyone has written a converter class that takes a dynamic object and generates a static class(es) out of it.

like image 677
AngryHacker Avatar asked Feb 22 '23 22:02

AngryHacker


2 Answers

Check out the JSON C# Class Generator:

This application generates C# classes from a sample JSON text, so you can use strongly typed programming with JSON. It currently supports typed arrays, typed objects, integers, floats, booleans, strings and nullable types.

like image 153
Chris Fulstow Avatar answered Mar 04 '23 01:03

Chris Fulstow


JavaScriptSerializer.ConvertToType is ideal for this. It maps matching properties to a static type, and ignores those that don't.

You do need to give it the static type, but I think converting a dynamic to a anon static is a code smell.

like image 39
TheCodeKing Avatar answered Mar 04 '23 03:03

TheCodeKing