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>
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.And use like so