Accessing child values by string name

  • Thread starter Thread starter Hugh O'Donnell
  • Start date Start date
H

Hugh O'Donnell

I am trying to access the values of child properties by a string
value.

For instance, have a user type "Form1.txtFirstName.Text" in a text
box, click on a button, and return the value of that field.

Is this possible to do?

My first thought was to use reflection, but
I can only seem to tell what properties are contained in the class,
and not their values.

Another thought was to serialize the object to an XmlDocument and
find the values that way... but many of the objects I want to do
this with are not serializable. :(

Any help you can give me would be GREATLY appreciated!

Thank You.

-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-

In order to reply via email, please remove the no_spam_for_me_ before
my real email address.
 
You do use reflection. To actually get the value of the property you
can use InvokeMember with BindingFlags.GetProperty and other
BindingFlags set.
 
Hugh,

With reflection when you get, for example, PropertyInfo object for a
propoerty by its name you can call GetValue of this PopretyInfo object to
read the value of the property. The same goes for fields.
 
Doug & Stoitcho,

Thank you for your replies. You have sent me on a path to try the
code below... however, p is always null. Could you possibly show
me a working example?

Thanks!

public string GetValueFromText(object o, string s)
{
Type t = o.GetType();
PropertyInfo p = t.GetProperty(s); // p is always null.
return p.ToString();
}

executing with:
GetValueFromText(Form1, "txtResult");
You do use reflection. To actually get the value of the property you
can use InvokeMember with BindingFlags.GetProperty and other
BindingFlags set.

-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-

In order to reply via email, please remove the no_spam_for_me_ before
my real email address.
 
...
Thank you for your replies. You have sent me on a path
to try the code below... however, p is always null.

I believe you've confused "Property" with "variable name" here...

I don't believe you have a property "txtResult" in the object Form1, and if
the latter even is an object and not a class?
public string GetValueFromText(object o, string s)
{
Type t = o.GetType();
PropertyInfo p = t.GetProperty(s); // p is always null.
return p.ToString();
}


If the naming in your form is from the default of what e.g. Visual
Studio.NET produces, then it's more likely that "txtResult" is the variable
name of a TextBox or a Label, something that in turn have a property "Text".

You then need yet another level of reflection, e.g. like this:


public string GetValueFromText(object o1, string member)
{
Type t1 = o1.GetType();

// In case the field is private, you need to
// provide binding flags...
FieldInfo f1 =
t1.GetField(member,
BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance);

// Now get the field's type
object o2 = f1.GetValue(o1);
Type t2 = o2.GetType();

// ...and get the "Text" property from that...
PropertyInfo p2 = t2.GetProperty("Text");
return p2.GetValue(o2, null).ToString();
}


// Bjorn A
 
Back
Top