I need to generate code using Expression trees that quickly fills out an array of structs T[]
where T contains a readonly field. I need to initialize it like after GetUninitializedObject() + IL or reflection-based setters.
UPDATE: At the moment it appears to be impossible. Please vote for it at MS Suggestions
struct Strct
{
public readonly int Value;
}
this code fails:
Expression.Assign(
Expression.Field(structByIndexFromArrayExp, "Value"),
deserializedValueExp)
During the expression tree construction, I get this error: Expression must be writeable
Which totally makes sense from the regular code perspective, but not during deserialization.
FormatterServices.GetUninitializedObject()
returns an object, which I would guess I need to avoid as it is boxed and therefore significantly slower.
What is the quickest way to initialize such struct arrays?
Update: At the moment the only realistic way I see is to dynamically generate a clone of struct T but without readonly attribute on fields, fill them out, fix both arrays in memory and do a memory copying. Please vote to tell Microsoft to fix it.