By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
458,107 Members | 1,221 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 458,107 IT Pros & Developers. It's quick & easy.

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

P: n/a
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
Nov 16 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
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

Nov 16 '05 #2

P: n/a
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" <mg*******@taxussi.no.com.spam.pl> wrote in message
news:co**********@nemesis.news.tpi.pl...
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

Nov 16 '05 #3

P: n/a
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

Nov 16 '05 #4

P: n/a
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" <mg*******@taxussi.no.com.spam.pl> wrote in message
news:co**********@nemesis.news.tpi.pl...
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

Nov 16 '05 #5

P: n/a
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
Nov 16 '05 #6

P: n/a
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

"Marcin Grzębski" <mg*******@taxussi.spamstop.com.spamstop.pl> wrote in
message news:co***********@mamut.aster.pl...
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

Nov 16 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.