Reputation: 21194
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 nullPropertyInfo.SetValue
failsAny ideas?
Upvotes: 4
Views: 1277
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