D.R.
D.R.

Reputation: 21194

Set auto-property's value via reflection

I've seen various threads on how to call a property's private setter via reflection. However, what about auto-properties without a setter?

public class Test
{
    public string Property { get; } = "";
}

Is it possible to set the value of this readonly property using reflection?

Any ideas?

Upvotes: 4

Views: 1277

Answers (1)

Jon Skeet
Jon Skeet

Reputation: 1499760

Is it possible to set the value of this readonly property using reflection?

No. Those properties are backed by read-only fields. There is no setter; any assignments performed in the constructor write directly to the fields.

If your design makes you want to write to a read-only property via reflection, you should revisit your design :)

Upvotes: 5

Related Questions