C# anonymous object with properties from dictionary

You can’t, basically. Anonymous types are created by the compiler, so they exist in your assembly with all the property names baked into them. (The property types aren’t a problem in this case – as an implementation detail, the compiler creates a generic type and then creates an instance of that using appropriate type arguments.)

You’re asking for a type with properties which are determined at execution time – which just doesn’t fit with how anonymous types work. You’d have to basically compile code using it at execution time – which would then be a pain as it would be in a different assembly, and anonymous types are internal…

Perhaps you should use ExpandoObject instead? Then anything using dynamic will be able to access the properties as normal.

Leave a Comment