How can i get the Property value using Reflection (Return type: Collection of controls)

M

Mudassar

i want to get the property value using reflection.

Scenerio:
i have a status bar on MDI form. it has property named "Panels" and i want
to get a specific panel from that
panels collection using reflection.
Please let me know.

Thanks
C# Developer
Mudassar
 
P

Pete Davis

Funny you should ask today. There's been a lot of reflection going on in our
office today. Here's a sample of getting a field:

string dataField;
FieldInfo fi = currentManager.GetType().GetField("dataField",
BindingFlags.NonPublic | BindingFlags.Instance);
dataField = (string) fi.GetValue(currentManager);


Basically, you'll want to get ProeprtyInfo instead of a FieldInfo (using
GetProperty()), but otherwise it's more or less the same.

In this case, currentManager is a "CurrencyManager" object and "dataField"
is a private field in the CurrencyManager.

The key is to make sure your BindingFlags values are correct. These should
work for non-public instance fields. If it's static or public (I doubt it's
public, otherwise why use reflection, right?), make the appropriate changes.

Hope that helps.

Pete
 

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

Top