我真的应该能够理解这个问题,但现在已经到了更容易询问的地步。
在C#函数中:
public static T GetValue<T>(String value) where T:new()
{
//Magic happens here
}
有什么好的方法来实现这个“magic”(神奇)吗?这个想法的背后是我需要解析xml,并且所需的值通常是原始类型(bool、int、string等),这是使用泛型的完美场所...但是一个简单的解决方案暂时困扰着我。
顺便说一下,这是我需要解析的xml样例:
<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>