我必须在XML下面反序列化。但是我必须将Sensor数据存储到动态加载的类中。 我怎么能这样做?
XML格式
<Root>
<Sensor id="0" Channel="1000">
<SensorName>RM-T</SensorName>
<SensorValue>148.00</SensorValue>
</Sensor>
<Sensor id="1" Channel="1001">
<SensorName>SAT1</SensorName>
<SensorValue>148.00</SensorValue>
</Sensor>
</Root>
存储反序列化数据的主类
[Serializable]
[XmlRoot(ElementName = "Root")]
public class Controller
{
public List<Sensor> SensorList
{
get;
set;
}
}
传感器类
public abstract class Sensor
{
String SensorName { get; set; }
String SensorValue { get; set; }
public abstract override String ToString();
}
RegularSensor Class
public class RegularSensor : Sensor
{
public override string ToString()
{
//Implementation goes here
}
}
在反序列化期间,我需要将传感器数据加载到RegularSensor类 我怎么能这样做?
答案 0 :(得分:-1)
这对你没关系:
public class RegularSensor : Sensor
{
Sensor mySensor = new Sernsor();
public override string ToString()
{
//Implementation goes here
}
}
然后反序列化此对象&#39; mySensor&#39;在函数中。您可以获得所有数据。