Access private member through reflection

  • Thread starter Thread starter Casey
  • Start date Start date
C

Casey

I have a custom control that allows user to place rtf content in it. The
custom control has a private member (RichTextBox). I have a code that
actually works:

MyRTFControl myRtf;

MemberInfo[] minfo = typeof(MyRTFControl).GetMember("richTextBox",
BindingFlags.FlattenHierarchy |
BindingFlags.IgnoreCase |
BindingFlags.Instance |
BindingFlags.NonPublic);

My question is how do I use minfo above to set some of the properties of
richTextBox ?
 
Hi,

1. Upcast minfo to either FieldInfo or PropertyInfo (depending on the nature
of the 'richTextBox' member)
2. Use the GetValue method to get the value of the field or of the property.
 
Back
Top