Reflection API

  • Thread starter Thread starter Skandy
  • Start date Start date
S

Skandy

Hello All:

I'm stuck with this. I'm trying to obtain the SelectedValue property
for a combobox control on UserControl using reflection.

So I write:
PropertyInfo pi =
objControl.GetType().GetMember("cmbComplex").GetType().GetProperty("SelectedValue");

This always returns a value null, and throws an exception hence.

When I debug through this using QuickWatch, I get to see that
objControl.GetType().GetMember("cmbComplex") evaluates to a ComboBox
indeed.

But why is that I'm unable to see the SelectedValue Property through
this?

Am I missing something obvious?

TIA.

Sk&y;
 
Hello All:

I'm stuck with this. I'm trying to obtain the SelectedValue property
for a combobox control on UserControl using reflection.

So I write:
PropertyInfo pi =
objControl.GetType().GetMember("cmbComplex").GetType().GetProperty("SelectedValue");

This always returns a value null, and throws an exception hence.

When I debug through this using QuickWatch, I get to see that
objControl.GetType().GetMember("cmbComplex") evaluates to a ComboBox
indeed.

But why is that I'm unable to see the SelectedValue Property through
this?

Am I missing something obvious?

TIA.

Sk&y;

First off all try to break up the statement. Is the returned combobox
really the required instance or not. Even though the QuickWatch
returns the type correct; you should (in sake of save coding)
determine if the getmember("cmb...") is not null. And you'll need that
object (combo) in the GetValue statement as well.

Further more is the selectedvalue property (you could see that in the
QuickWatch) public available?

The PropertyInfo (for the value that is) should be queried for the
value:

Code:
PropertyInfo oInfo = oTestObj.GetType().GetProperty("SelectedValue");
int iValue = (int)oInfo.GetValue(oTestObj, null);

in the code above the oTestObj is your combobox.

greetings
Leon
 
Hi leon:

Thanks for the reply.

I just verified that I dont have an object of type
System.Windows.Forms.ComboBox returned by the call:

objControl.GetType().GetMember("cmbComplex").

The above call returns an object of type MemberInfo.
Now given this fact, is there anyway that I can get a handle of the
ComboBox, something like:

ComboBox cmb = (ComboBox)mi.......

where mi is the member variable.

I dont seem to get this.

Thanks again.
Sk&y;
 
Hello:

Got that one working.

Split it as:

FieldInfo objFieldInfo =
(FieldInfo)objControl.GetType().GetMember("cmbComplex")[0];
ComboBox objCombo =
(ComboBox)objFieldInfo.GetValue(objControl);
objCombo.SelectedValue = objReader.GetValue(0);


and its all working now!

Thanks again for looking into my problem!

regards,
Sk&y;
 
Hi leon:

Thanks for the reply.

I just verified that I dont have an object of type
System.Windows.Forms.ComboBox returned by the call:

objControl.GetType().GetMember("cmbComplex").

The above call returns an object of type MemberInfo.
Now given this fact, is there anyway that I can get a handle of the
ComboBox, something like:

ComboBox cmb = (ComboBox)mi.......

where mi is the member variable.

I dont seem to get this.

Thanks again.
Sk&y;

The easy way around this: create a property which returns your
combobox (public) and get that value using
"GetProperty(...)...GetValue(...)"

Otherwise I assume, never got into this that deep, you need to fetch
your combobox from the (nested)controls collection from the
usercontrol/form (in your case usercontrol).

Leon
 
Hi Leon:

I agree that the way to obtain Property values is through the
PropertyInfo class.

But what I was trying to achieve here is to obtain the combobox's
reference from my user control. The following code got me just that.

FieldInfo objFieldInfo =
(FieldInfo)objControl.GetType().GetMember("cmbComplex")[0];
ComboBox objCombo =
(ComboBox)objFieldInfo.GetValue(objControl);

The trouble that I was facing was that the objControl's reference is
not known in the class that I'm coding in and I was wondering how to
obtain the combo's reference.

Thanks again!
 
Back
Top