Using Reflection to get a Field Value from Control

G

Guest

I am writing a simulation program, the main classes inherit from a base class
which itself inherits from Control. I am using reflection to serialize these
objects including the fields from control. This works perfectly apart from
the field text in Control which always comes back null. Although the IDE
always shows it having the correct value. Since the IDE presumably uses
reflection to show the value, I am at a loss to know why this one field is a
problem. Interestingly, the command window also comes back with a null, when
doing the same operation, but if you do the operation on the original object
just after creation it is correct. It is worth noting the objects are placed
in a Collection before serialization. So it is using FieldInfo.GetValue on
the items in the Collection which is failing on the string field.
 
N

Nicholas Paldino [.NET/C# MVP]

Tony,

Are you reflecting on the field, or on the property? I don't think that
most controls store the text themselves. Rather, when you call the Text
property, it calls SendMessage with a WM_GETTEXT message to get the text
contents of the control, which is why the text field would be blank.

Hope this helps.
 
G

Guest

I am reflecting on the field text which provides the property Text, this
works when done at the form level but not during serialization. In fact it is
more complicated than that. I set up a test program, that inherited a very
simple class from control and found that it picked up the field properly,
using exactly the same class for serialization as I am using in the main
program. The field is certainly there in the main program but comes up as
null, in the test program it comes up properly. All of which leaves me at a
loss as to what is going on.

Nicholas Paldino said:
Tony,

Are you reflecting on the field, or on the property? I don't think that
most controls store the text themselves. Rather, when you call the Text
property, it calls SendMessage with a WM_GETTEXT message to get the text
contents of the control, which is why the text field would be blank.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

tony lock said:
I am writing a simulation program, the main classes inherit from a base
class
which itself inherits from Control. I am using reflection to serialize
these
objects including the fields from control. This works perfectly apart from
the field text in Control which always comes back null. Although the IDE
always shows it having the correct value. Since the IDE presumably uses
reflection to show the value, I am at a loss to know why this one field is
a
problem. Interestingly, the command window also comes back with a null,
when
doing the same operation, but if you do the operation on the original
object
just after creation it is correct. It is worth noting the objects are
placed
in a Collection before serialization. So it is using FieldInfo.GetValue on
the items in the Collection which is failing on the string field.
 
C

cody

You can never assume that the field text is properly set. Therefore you
should use the property Text which should always be set correctly.


tony lock said:
I am reflecting on the field text which provides the property Text, this
works when done at the form level but not during serialization. In fact it is
more complicated than that. I set up a test program, that inherited a very
simple class from control and found that it picked up the field properly,
using exactly the same class for serialization as I am using in the main
program. The field is certainly there in the main program but comes up as
null, in the test program it comes up properly. All of which leaves me at a
loss as to what is going on.

Nicholas Paldino said:
Tony,

Are you reflecting on the field, or on the property? I don't think that
most controls store the text themselves. Rather, when you call the Text
property, it calls SendMessage with a WM_GETTEXT message to get the text
contents of the control, which is why the text field would be blank.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

tony lock said:
I am writing a simulation program, the main classes inherit from a base
class
which itself inherits from Control. I am using reflection to serialize
these
objects including the fields from control. This works perfectly apart from
the field text in Control which always comes back null. Although the IDE
always shows it having the correct value. Since the IDE presumably uses
reflection to show the value, I am at a loss to know why this one field is
a
problem. Interestingly, the command window also comes back with a null,
when
doing the same operation, but if you do the operation on the original
object
just after creation it is correct. It is worth noting the objects are
placed
in a Collection before serialization. So it is using FieldInfo.GetValue on
the items in the Collection which is failing on the string field.
 

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