473,387 Members | 1,464 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

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

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
6 5793
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: dotnetguy | last post by:
Hello, having a FieldInfo object I'd like to get a reference to the corresponding field object. I know the type of the field object (so I can do a cast if it is required) but I don't know if...
2
by: Max | last post by:
I just wonder if there is a way of converting an object to a string. I am using a function to send an object parameter e.g "fctng(train0);" later on I would like to use train0 but as a string to...
2
by: Abdessamad Belangour | last post by:
Hi, I want to get the visiblity kind of a FieldInfo object. The "IsPublic" ans "IsPrivate" properties give the required information for public and private visibilities. My questions are: 1. What...
0
by: ORC | last post by:
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...
5
by: tienlx | last post by:
Hi all, I'm have a problem that i don't know how to convert object to char in C# . Does anybody know ? Thanks.
0
by: bacsumu | last post by:
i'm programming in c# i use embeded webbrowser ----------------------------------------------------- int Exec( ref Guid pguidCmdGroup, int nCmdID, int nCmdexecopt,
0
by: suneethadotnet | last post by:
hi , object objee= obj1.GetSecurityInfo("hi"); here GetSecurityInfo("hi") returns struct.i am assigning it to object.now the object objee have all the properties of GetSecurityInfo("hi")...
5
by: deepthisoft | last post by:
hi, I have converted vector to object. How can I convert Object into Vector. My code look like this, Vector column=new Vector(); column.add("one");
2
by: Charming12 | last post by:
Hi All, I am stuck with a unique problem, I have a class whose object i need to convert to a byte to send it further. I am using microsoft's Binaryformatter to convert object to byte as per...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

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.