[英]How can I modify my class to override a list add?
I am using the following class that was suggested to me to track timings: 我正在使用向我建议的以下课程来跟踪时间:
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;
}
}
Here's my class that holds the list of times: 这是我的上课时间列表:
public abstract class BaseGridViewModel
{
protected BaseGridViewModel()
{
Times = new List<TimeEvent>();
}
public IList<TimeEvent> Times {get; set;}
}
Here's how a start the stopwatch and add an event: 这是启动秒表并添加事件的方法:
var sw = Stopwatch.StartNew();
vm.Times.Add(new TimeEvent(sw.ElapsedMillisends, "After event 1"));
sw.Stop();
Is there a way that I could make it so that I have a method inside my viewmodel that I could pass the values to and it would then do the same as the Add internally. 有没有办法可以做到这一点,以便在我的视图模型中有一个可以将值传递给它的方法,然后该方法将在内部执行与Add相同的操作。 For example a method like this:
例如这样的方法:
vm.Event(sw, "After event 1"));
Even 甚至
Why not? 为什么不?
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));
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.