473,606 Members | 2,381 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to convert in FieldInfo.Setva lue(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_T he_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 5809
Hi ORC,

"FieldInfo" has a "DeclaringT ype" 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.Declar ingType.GetMeth od("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_T he_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.ChangeT ype(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*******@taxu ssi.no.com.spam .pl> wrote in message
news:co******** **@nemesis.news .tpi.pl...
Hi ORC,

"FieldInfo" has a "DeclaringT ype" 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.Declar ingType.GetMeth od("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_T he_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 "DeclaringT ype" 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.Declar ingType.GetMeth od("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_T he_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.ChangeT ype(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*******@taxu ssi.no.com.spam .pl> wrote in message
news:co******** **@nemesis.news .tpi.pl...
Hi ORC,

"FieldInfo" has a "DeclaringT ype" 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.Declar ingType.GetMeth od("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_T he_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.ChangeT ype(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.Change Type(...)" 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(stri ng 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*******@taxu ssi.spamstop.co m.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.ChangeT ype(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.Change Type(...)" 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(stri ng 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
1417
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 FieldInfo indeed provides a reference to the underlying object and how to get it. Can anyone help out??? Thx.
2
52271
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 access a img id called <img id="train0"> . This might be obvious to you but I am not a programmer, just trying to learn javascript at home for fun. Thanks Max
2
1911
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 about "protected" visibility? 2. What exactly do we mean by Family ? Is it a hierarchy of deriving classes? 3. If a field is IsFamily, does it mean it's protected? 4. I tried to get some information through the example of msdn at :...
0
338
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 field in MyClass.GetType().GetFields() ) { field.SetValue(MyClass, String_From_XML_Representing_The_Field_Value); }
5
6777
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
1421
by: bacsumu | last post by:
i'm programming in c# i use embeded webbrowser ----------------------------------------------------- int Exec( ref Guid pguidCmdGroup, int nCmdID, int nCmdexecopt,
0
1218
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") struct.but am not getting when i type objee.
5
17394
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
6008
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 given below. But problem here is that the converted byte contains 245 bytes of value instead of 22 what the class actually should contain. private byte ObjectToByteArray(Object obj) {
0
8016
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
7955
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8431
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8096
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
5966
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5466
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
3980
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1557
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1300
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.