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?

  • PropertyInfo.SetMethod returns null
  • PropertyInfo.SetValue fails

Any ideas?

Jon Skeet
people
quotationmark

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 :)

people

See more on this question at Stackoverflow