从Serializationinfo商店反序列化枚举?

时间:2017-10-11 21:37:36

标签: c# serialization enums

我有一个名为UnitsManager的班级。在这个类中是一个枚举:

public enum TimeUnit
{
    Seconds,
    Milliseconds,
    Minutes,
    Hours,
    Days,
    Months,
    Years
}

此枚举用于另一个名为Process的类中。它有一个属性:

    private TimeUnit cycleTimeUnit = TimeUnit.Seconds; 
    [XmlAttribute("CycleTimeUnit")]
    public TimeUnit CycleTimeUnit
    {
        get { return this.cycleTimeUnit; }
        set { this.cycleTimeUnit = value; }
    }

我正在尝试序列化和反序列化Process类。我在Process

上实现了ISerializable
    public override void GetObjectData(SerializationInfo info, StreamingContext context)
    {
        base.GetObjectData(info, context);

        info.AddValue("CycleTime", CycleTime);
        info.AddValue("CycleTimeUnit", CycleTimeUnit);
    }

我的问题来自反序列化

    private Process(SerializationInfo info, StreamingContext context)
    {
        CycleTime = info.GetDouble("CycleTime");
        CycleTimeUnit = info.????("CycleTimeUnit"); // <--- ???
    }

如何将其转换为CycleTimeUnit类型?

1 个答案:

答案 0 :(得分:3)

试试这个:

<div id="element1">element1</div>
<div id="element2">element2</div>
<div id="element3">element3</div>