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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top