object = (string + type) ?
-
Hey everybody! I have a question. I'm trying to do some dynamic property loading from an xml file. So i know the following things from the xml: 1) the type of the object to initialize 1) what property of the object to initialize 2) the value the property should get in the form of a string 3) the type of the property (i got that one from the propertyinfo) I know there has to be a method that you pass a type and a string, and it creates the object for you, just like the propertygrid. I don't want to write a huge switch statement... Example:
// I know how to get the 5.4 as a string, but not how to make it a float...
<Roundness>5.4</Roundness> ---> blabla.Roundness = 5.4!Thank you! :)
"..Commit yourself to quality from day one..it's better to do nothing at all than to do something badly.." -- Mark McCormick
|| Fold With Us! || Pensieve || VG.Net ||
-
Hey everybody! I have a question. I'm trying to do some dynamic property loading from an xml file. So i know the following things from the xml: 1) the type of the object to initialize 1) what property of the object to initialize 2) the value the property should get in the form of a string 3) the type of the property (i got that one from the propertyinfo) I know there has to be a method that you pass a type and a string, and it creates the object for you, just like the propertygrid. I don't want to write a huge switch statement... Example:
// I know how to get the 5.4 as a string, but not how to make it a float...
<Roundness>5.4</Roundness> ---> blabla.Roundness = 5.4!Thank you! :)
"..Commit yourself to quality from day one..it's better to do nothing at all than to do something badly.." -- Mark McCormick
|| Fold With Us! || Pensieve || VG.Net ||
I'm not exactly sure but do you probably mean
Convert.ChangeType(myString, myType);
? -
I'm not exactly sure but do you probably mean
Convert.ChangeType(myString, myType);
?That was not the method i was looking for, because you can't convert everything from a string that way. But it did help me in the right direction, so thank you very much! :-D I ended up writing a method by myself, it wasn't so hard as i thought it would be. :)
"..Commit yourself to quality from day one..it's better to do nothing at all than to do something badly.." -- Mark McCormick
|| Fold With Us! || Pensieve || VG.Net ||
-
Hey everybody! I have a question. I'm trying to do some dynamic property loading from an xml file. So i know the following things from the xml: 1) the type of the object to initialize 1) what property of the object to initialize 2) the value the property should get in the form of a string 3) the type of the property (i got that one from the propertyinfo) I know there has to be a method that you pass a type and a string, and it creates the object for you, just like the propertygrid. I don't want to write a huge switch statement... Example:
// I know how to get the 5.4 as a string, but not how to make it a float...
<Roundness>5.4</Roundness> ---> blabla.Roundness = 5.4!Thank you! :)
"..Commit yourself to quality from day one..it's better to do nothing at all than to do something badly.." -- Mark McCormick
|| Fold With Us! || Pensieve || VG.Net ||
System.ComponentModel.TypeConverter tc = System.ComponentModel.TypeDescriptor.GetConverter(typeof(ElementType)); Then use tc.ConvertFromInvariantString / tc.ConvertToInvariantString. Works for int, float, Point, Font, etc...
-
System.ComponentModel.TypeConverter tc = System.ComponentModel.TypeDescriptor.GetConverter(typeof(ElementType)); Then use tc.ConvertFromInvariantString / tc.ConvertToInvariantString. Works for int, float, Point, Font, etc...
Ah TypeDescriptor, that's the one! That is a lot easier than looking for TypeConverterAttributes and stuff. Thank you so much man! :jig:
"..Commit yourself to quality from day one..it's better to do nothing at all than to do something badly.." -- Mark McCormick
|| Fold With Us! || Pensieve || VG.Net ||