471,317 Members | 1,500 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,317 software developers and data experts.

Accessing child values by string name

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.
Dec 16 '05 #1
4 1296
You do use reflection. To actually get the value of the property you
can use InvokeMember with BindingFlags.GetProperty and other
BindingFlags set.

Dec 16 '05 #2
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.
--

Stoitcho Goutsev (100) [C# MVP]

"Hugh O'Donnell" <no***********************@odonnell.tv> wrote in message
news:Xn**********************************@24.94.17 0.102...
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.

Dec 16 '05 #3
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.
Dec 16 '05 #4

"Hugh O'Donnell" wrote...
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
Dec 16 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

6 posts views Thread by Chris Styles | last post: by
4 posts views Thread by SteveT | last post: by
reply views Thread by rosydwin | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.