Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the value of non- public members of picturebox?

Tags:

c#

I need to get the value of Image rectangle from Non public members of picturebox.

How to get that value?

Thanks in advance.

like image 812
Nivas Avatar asked Jul 22 '10 09:07

Nivas


2 Answers

This is how to get the value, using reflection:

PropertyInfo pInfo = pictureBox1.GetType().GetProperty("ImageRectangle", 
    System.Reflection.BindingFlags.Public | 
    System.Reflection.BindingFlags.NonPublic | 
    System.Reflection.BindingFlags.Instance);

Rectangle rectangle = (Rectangle)pInfo.GetValue(pictureBox1, null);

Although, as Jon has said, there may be a better way of achieving what you're trying to do. Accessing private members through reflection is usually a pretty big code smell.

like image 191
djdd87 Avatar answered Oct 23 '22 13:10

djdd87


Well, you could do it with reflection... but you shouldn't. It's not clear exactly what you mean by "value of Image rectangle" but you should definitely try to do all this through the public API. What are you trying to achieve? There may be a different way.

EDIT: Okay, now I see the property you're trying to access... you may be interested in this Connect issue filed in 2004. You're not the only one to want this... although whether you need it for the same reason or not, I don't know.

like image 21
Jon Skeet Avatar answered Oct 23 '22 12:10

Jon Skeet