How to convert in FieldInfo.Setvalue(Object, Object) ?

O

ORC

I will use FieldInfo to import values from a XML file into the fields of an
objects. But how to convert to the proper type like in this:

public void Load(object MyClass)
{
foreach( FieldInfo field in MyClass.GetType().GetFields() )
{
field.SetValue(MyClass,
String_From_XML_Representing_The_Field_Value);
}
}

The SetValue method doesn't automatically convert to the correct type and
therefore raises an error, so how do I do that?

Mvh
Ole
 
?

=?ISO-8859-2?Q?Marcin_Grz=EAbski?=

Hi ORC,

"FieldInfo" has a "DeclaringType" property.
If that type is simple type the you can write
simlpe "switch()" statement.
On the other way, you can use that type to find out the
right "Parse()" method.

e.g. by:
MethodInfo mi=field.DeclaringType.GetMethod("Parse", new Type[] {
typeof(string)});

HTH
Marcin
 
O

ORC

Hi,

Thanks. I've been studying since I wrote my question and have found that
this might solve the problem in an easy and clean way:
field.SetValue(MyClass, Convert.ChangeType(param, field.FieldType,
??Iprovider??));

But as you see from the '??' in the above I'm stucked in 'Iprovider'
parameter. The reason for using this is that it is the only overload of
ChangeType supported in Compact Framework.

So now my question is: How do I use the Iprovider ???? I know it has to do
with culture dependency, but how to handle it so that the ChangeType allways
will use the dot as a dicimal symbol no matter what the OS is set to?

Thank you for your help!
Ole

Marcin Grzêbski said:
Hi ORC,

"FieldInfo" has a "DeclaringType" property.
If that type is simple type the you can write
simlpe "switch()" statement.
On the other way, you can use that type to find out the
right "Parse()" method.

e.g. by:
MethodInfo mi=field.DeclaringType.GetMethod("Parse", new Type[] {
typeof(string)});

HTH
Marcin
I will use FieldInfo to import values from a XML file into the fields of an
objects. But how to convert to the proper type like in this:

public void Load(object MyClass)
{
foreach( FieldInfo field in MyClass.GetType().GetFields() )
{
field.SetValue(MyClass,
String_From_XML_Representing_The_Field_Value);
}
}

The SetValue method doesn't automatically convert to the correct type and
therefore raises an error, so how do I do that?

Mvh
Ole
 
?

=?ISO-8859-2?Q?Marcin_Grz=EAbski?=

Hi Ole,
Hi,

Thanks. I've been studying since I wrote my question and have found that
this might solve the problem in an easy and clean way:
field.SetValue(MyClass, Convert.ChangeType(param, field.FieldType,
??Iprovider??));

But as you see from the '??' in the above I'm stucked in 'Iprovider'
parameter. The reason for using this is that it is the only overload of
ChangeType supported in Compact Framework.

So now my question is: How do I use the Iprovider ???? I know it has to do
with culture dependency, but how to handle it so that the ChangeType allways
will use the dot as a dicimal symbol no matter what the OS is set to?

Thank you for your help!
Ole

If your "param" is the string representation of field's value
then i don't think that "Convert.ChangeType(...)" is the right
solution for your problem.

If i'm wrong about "param" then don't read the next part.

If your "field" type is a Type that has a static "Parse(string str)"
method then you could "Ivoke" that method with "param" parameter
to get "field" value. Elswere you can write more sophisticated
algorithm for a string conversion.

HTH
Marcin
 
O

ORC

Hi Marcin,

Thanks. I found a soultion to all that - try to have a look at thread:
"Regional settings in XML ?" dated november 27. and sent from me (ORC) -
it's also in this csharp newsgroup.

BR
Ole
 

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