How C # sets a read-only property when initializing an anonymous object

In C #, object initializers can set read-only public fields and properties.

However, with anonymous types, properties are read-only. So how does .NET set them to initialize an object?

+3
source share
3 answers

An object initializer for anonymous types does not behave like a regular object initializer. those. it does not set properties directly. It translates into a constructor call that can change read-only fields.

+3
source

Like all read-only properties, they can be set in the constructor.

, .

+7

, .

eg. just enter the fields with the corresponding binding flags, and then list and call SetValue with the correct parameters ...

+1
source

All Articles