Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Converting from String to <T>

Tags:

c#

xml

generics

I really should be able to get this, but I'm just to the point where I think it'd be easier to ask.

In the C# function:

public static T GetValue<T>(String value) where T:new() {    //Magic happens here } 

What's a good implementation for the magic? The idea behind this is that I have xml to parse and the desired values are often primitives (bool, int, string, etc.) and this is the perfect place to use generics... but a simple solution is eluding me at the moment.

btw, here's a sample of the xml I'd need to parse

<Items>     <item>         <ItemType>PIANO</ItemType>         <Name>A Yamaha piano</Name>         <properties>             <allowUpdates>false</allowUpdates>             <allowCopy>true</allowCopy>         </properties>        </item>     <item>         <ItemType>PIANO_BENCH</ItemType>         <Name>A black piano bench</Name>         <properties>             <allowUpdates>true</allowUpdates>             <allowCopy>false</allowCopy>             <url>www.yamaha.com</url>         </properties>     </item>     <item>         <ItemType>DESK_LAMP</ItemType>         <Name>A Verilux desk lamp</Name>         <properties>             <allowUpdates>true</allowUpdates>             <allowCopy>true</allowCopy>             <quantity>2</quantity>         </properties>     </item> </Items> 
like image 931
Saint Domino Avatar asked Apr 09 '09 03:04

Saint Domino


People also ask

How can a string be converted to a number?

You convert a string to a number by calling the Parse or TryParse method found on numeric types ( int , long , double , and so on), or by using methods in the System. Convert class. It's slightly more efficient and straightforward to call a TryParse method (for example, int.

How do I convert a string to a number in C++?

One effective way to convert a string object into a numeral int is to use the stoi() function. This method is commonly used for newer versions of C++, with is being introduced with C++11. It takes as input a string value and returns as output the integer version of it.

How does convert ChangeType work?

ChangeType(Object, TypeCode) is a general-purpose conversion method that converts the object specified by value to a predefined type specified by typeCode . The value parameter can be an object of any type.


Video Answer


2 Answers

I would suggest instead of trying to parse XML yourself, you try to create classes that would deserialize from the XML into the classes. I would strongly recommend following bendewey's answer.

But if you cannot do this, there is hope. You can use Convert.ChangeType.

public static T GetValue<T>(String value) {   return (T)Convert.ChangeType(value, typeof(T)); } 

And use like so

GetValue<int>("12"); // = 12 GetValue<DateTime>("12/12/98"); 
like image 194
Samuel Avatar answered Oct 09 '22 16:10

Samuel


You can start with something roughly like this:

TypeConverter converter = TypeDescriptor.GetConverter(typeof(T)); if (converter != null) {    return (T)converter.ConvertFrom(value); } 

If you have to parse attributes that are special types, like colors or culture strings or whatnot, you will of course have to build special cases into the above. But this will handle most of your primitive types.

like image 41
womp Avatar answered Oct 09 '22 18:10

womp