Set auto-property value through reflection

I have seen various topics on how to call a private setter property through 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

    does not work

Any ideas?

+3


source to share


1 answer


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

Not. These properties are supported by read-only fields. No setter; any assignments made in the constructor are written directly to the fields.



If your project forces you to write to a read-only property through reflection, you should rethink your design :)

+5


source







All Articles