At runtime, how can I test whether a Property is readonly?

后端 未结 4 420
日久生厌
日久生厌 2021-01-05 01:06

I am auto-generating code that creates a winform dialog based on configuration (textboxes, dateTimePickers etc). The controls on these dialogs are populated from a saved da

4条回答
  •  执笔经年
    2021-01-05 01:38

    I noticed that when using PropertyInfo, the CanWrite property is true even if the setter is private. This simple check worked for me:

    bool IsReadOnly = prop.SetMethod == null || !prop.SetMethod.IsPublic;
    

提交回复
热议问题