如何修改我的类以覆盖列表添加?

时间:2012-09-13 02:03:35

标签: c#

我正在使用以下课程建议我跟踪时间:

public sealed class TimeEvent
{
  public long Elapsed {get; private set;}
  public string Description { get; private set;}
  public TimeEvent(long elapsedTime, string descriptionOfEvent)
  {
    this.Elapsed = elapsedTime;
    this.Description = descriptionOfEvent;
  }
}

这是我的班级,其中包含时间列表:

public abstract class BaseGridViewModel
{

    protected BaseGridViewModel()
    {
        Times = new List<TimeEvent>();
    }
    public IList<TimeEvent> Times {get; set;}

}

以下是启动秒表并添加事件的方法:

var sw = Stopwatch.StartNew();
vm.Times.Add(new TimeEvent(sw.ElapsedMillisends, "After event 1"));
sw.Stop();

有没有一种方法可以让我在我的viewmodel中有一个方法,我可以将值传递给它,然后它会像内部添加一样。例如,像这样的方法:

vm.Event(sw, "After event 1"));

甚至

1 个答案:

答案 0 :(得分:5)

为什么不呢?

public abstract class BaseGridViewModel
{

    protected BaseGridViewModel()
    {
        Times = new List<TimeEvent>();
    }
    public IList<TimeEvent> Times {get; set;}

    public void Event(StopWatch watch, string message)
    {
        Times.Add(new TimeEvent(watch.ElapsedMilliseconds, message));
    }

}