Reflection Question

  • Thread starter Thread starter John
  • Start date Start date
J

John

I have a class the reads in a file and sets the values of the file into its
properties. This class is used to populate the data onto a form. This form
has controls created at runtime based on user input (file values when file
is opened). I was hoping to put the name of the property in the tag of each
control and fire an event if the a file is open when the control is created
and use the tag to get the correct property from the class. Can I use
reflection to do this? Or is there a better way to do this?

Thanks
 
Hi John,

Why don't you (instead of using reflection) go thru the form's Controls
collection and check the tag of the controls. Anyway some conrols may not be
found in the form's collection rather they might reside in some of the
controls' Control's collection and so forth.

Using reflection you have to go thru all fields of the form (some of them
will be controls some of them not) and check if the type of the field is
descendant of the Control class and if it is get the Tag value and so on.

I think the first solution is easier.

HTH
B\rgds
100
 
Thanks for the post. I do go through the form's control collection and get
the tag. Now that I have the tag value (which is the name of the property
in the class that contains the file's values) and need to use to get the
property. How can I do this?

Thanks
 
Thanks for the post. I guess your right I could just have a method in the
class. But wouldnt I have to have a long switch statement?

Thanks
 
Hi John,
Ok, I got it.

you can do the following

Assuming the properties are public and instance (not static) and say you
have:
string tag;
ValuesClass values;

PropertyInfo pi = values.GetType().GetProperty(propname);
object propValue = pi.GetValue(values);
If my assumptions are not correct you have to provide appropriate binding
flags to GetProperty method.

Or instead of using reflection why don't you just provide a method in the
class with file's values which will return value from name

object GetValue(string name);

then you can just do
value = values.GetValue(tag);

HTH
B\rgds
100
 
Hi John,
But wouldnt I have to have a long switch statement?

Maybe you would.

It depends on how you store your values.
If they have been already read from the file at the moment when you call
that GetValue method you might be able to store them in a hashtable using
the tag as a key.
Then GetValue method will look like

object GetValue(string tag)
{
return valueStorage[tag]; //valueStorage is of type Hashtable
}


HTH
B\rgds
100
 
Thanks for your help. I would create a hashtable however the file I am
reading in is a xml file and I am not looping through it. I am just
deserializing it. I am using the GetField().GetValue to retrieve the value.
Thanks again.

100 said:
Hi John,
But wouldnt I have to have a long switch statement?

Maybe you would.

It depends on how you store your values.
If they have been already read from the file at the moment when you call
that GetValue method you might be able to store them in a hashtable using
the tag as a key.
Then GetValue method will look like

object GetValue(string tag)
{
return valueStorage[tag]; //valueStorage is of type Hashtable
}


HTH
B\rgds
100

John said:
Thanks for the post. I guess your right I could just have a method in the
class. But wouldnt I have to have a long switch statement?

Thanks

field I
use
 
Hi lan,

What is the difficulty of this deserialize?
After you deserialize the xml file, you should have got the class object of
all the values. Then you can use reflection to get the corresponding values
by property name.
Also, you can do your own deserialize to store the xml key/value pair into
hashtable.

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.

--------------------
| From: "Ian" <[email protected]>
| References: <#[email protected]>
<[email protected]>
<[email protected]>
<#[email protected]>
<#[email protected]>
<[email protected]>
| Subject: Re: Reflection Question
| Date: Thu, 13 Nov 2003 14:08:50 -0700
| Lines: 150
| X-Priority: 3
| X-MSMail-Priority: Normal
| X-Newsreader: Microsoft Outlook Express 6.00.2800.1158
| X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1165
| Message-ID: <#[email protected]>
| Newsgroups: microsoft.public.dotnet.languages.csharp
| NNTP-Posting-Host: 64.207.45.37
| Path: cpmsftngxa06.phx.gbl!TK2MSFTNGP08.phx.gbl!tk2msftngp13.phx.gbl
| Xref: cpmsftngxa06.phx.gbl microsoft.public.dotnet.languages.csharp:199164
| X-Tomcat-NG: microsoft.public.dotnet.languages.csharp
|
| Thanks for your help. I would create a hashtable however the file I am
| reading in is a xml file and I am not looping through it. I am just
| deserializing it. I am using the GetField().GetValue to retrieve the
value.
| Thanks again.
|
| | > Hi John,
| > > But wouldnt I have to have a long switch statement?
| >
| > Maybe you would.
| >
| > It depends on how you store your values.
| > If they have been already read from the file at the moment when you call
| > that GetValue method you might be able to store them in a hashtable
using
| > the tag as a key.
| > Then GetValue method will look like
| >
| > object GetValue(string tag)
| > {
| > return valueStorage[tag]; //valueStorage is of type Hashtable
| > }
| >
| >
| > HTH
| > B\rgds
| > 100
| >
| > | > > Thanks for the post. I guess your right I could just have a method in
| the
| > > class. But wouldnt I have to have a long switch statement?
| > >
| > > Thanks
| > >
| > > | > > > Hi John,
| > > > Ok, I got it.
| > > >
| > > > you can do the following
| > > >
| > > > Assuming the properties are public and instance (not static) and say
| you
| > > > have:
| > > > string tag;
| > > > ValuesClass values;
| > > >
| > > > PropertyInfo pi = values.GetType().GetProperty(propname);
| > > > object propValue = pi.GetValue(values);
| > > > If my assumptions are not correct you have to provide appropriate
| > binding
| > > > flags to GetProperty method.
| > > >
| > > > Or instead of using reflection why don't you just provide a method
in
| > the
| > > > class with file's values which will return value from name
| > > >
| > > > object GetValue(string name);
| > > >
| > > > then you can just do
| > > > value = values.GetValue(tag);
| > > >
| > > > HTH
| > > > B\rgds
| > > > 100
| > > > | > > > > Thanks for the post. I do go through the form's control
collection
| > and
| > > > get
| > > > > the tag. Now that I have the tag value (which is the name of the
| > > property
| > > > > in the class that contains the file's values) and need to use to
get
| > the
| > > > > property. How can I do this?
| > > > >
| > > > > Thanks
| > > > >
| > > > > | > > > > > Hi John,
| > > > > >
| > > > > > Why don't you (instead of using reflection) go thru the form's
| > > Controls
| > > > > > collection and check the tag of the controls. Anyway some
conrols
| > may
| > > > not
| > > > > be
| > > > > > found in the form's collection rather they might reside in some
of
| > the
| > > > > > controls' Control's collection and so forth.
| > > > > >
| > > > > > Using reflection you have to go thru all fields of the form
(some
| of
| > > > them
| > > > > > will be controls some of them not) and check if the type of the
| > field
| > > is
| > > > > > descendant of the Control class and if it is get the Tag value
and
| > so
| > > > on.
| > > > > >
| > > > > > I think the first solution is easier.
| > > > > >
| > > > > > HTH
| > > > > > B\rgds
| > > > > > 100
| > > > > >
| > > > > > | > > > > > > I have a class the reads in a file and sets the values of the
| file
| > > > into
| > > > > > its
| > > > > > > properties. This class is used to populate the data onto a
| form.
| > > > This
| > > > > > form
| > > > > > > has controls created at runtime based on user input (file
values
| > > when
| > > > > file
| > > > > > > is opened). I was hoping to put the name of the property in
the
| > tag
| > > > of
| > > > > > each
| > > > > > > control and fire an event if the a file is open when the
control
| > is
| > > > > > created
| > > > > > > and use the tag to get the correct property from the class.
Can
| I
| > > use
| > > > > > > reflection to do this? Or is there a better way to do this?
| > > > > > >
| > > > > > > Thanks
| > > > > > >
| > > > > > >
| > > > > >
| > > > > >
| > > > >
| > > > >
| > > >
| > > >
| > >
| > >
| >
| >
|
|
|
 
Back
Top