如何序列化一个system . timer . timer

本文关键字:timer 一个 system 序列化 | 更新日期: 2023-09-27 18:05:37

是否有办法使System.Timers.Timer可序列化

如何序列化一个system . timer . timer

我在Timer类中没有看到任何Serializable属性

但是你可以做这样的类(很好的教程'Serialization (c#和Visual Basic)'):

[Serializable]
public class CoolTimerWhatIWantToSerializeButForWhat
{
    public double LocalInterval { get; set; }
    public void SaveSomePropertiesOfTimer(System.Timers.Timer iWannaIt)
    {
        //save to local properties properties of iWannaIt
        LocalInterval = iWannaIt.Interval;
    }
    public System.Timers.Timer GetItBackNOW()
    {
        return new System.Timers.Timer(LocalInterval);
    }
}

class Timer未暴露为'Serializable'.

一个选项是将对象标记为[NonSerialized].