使用Timer知道对象的年龄

use*_*240 1 c#

我正在写一个名为Food的课程.我考虑了一个字段"int cookingTime"来查看这个类的每个实例已存在多长时间.我以为我应该使用Timer类.我写了下面的一段代码,但它没有用.我不确定我的方法是否正确.我非常感谢任何帮助.

Class Food
{ 

    private System.Timers.Timer timerClock = new System.Timers.Timer();

    static int cookingTime = 0;

    public Food()
       {

           this.timerClock.Elapsed += new System.Timers.ElapsedEventHandler(process);
           this.timerClock.Interval = 1000;
           this.timerClock.Enabled = true;

    }

    static void process(Object myObject, EventArgs myEventArgs)
    {

        cookingTime += 1;
    }

}
Run Code Online (Sandbox Code Playgroud)

Las*_*ert 5

如何保存对象创建的时间戳?然后当你想知道对象存在多长时,只需从Now()中删除它.