简体   繁体   English

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

[英]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.

相关问题 如何在单独的 class 中为方法覆盖或添加属性,以便刷新 WCF 服务不会影响我的更改? - How can I override or add an attribute to a method in a separate class so that refreshing a WCF service doesn't blow away my changes? 如何覆盖 ColorEditor class 的 ColorUI class 并添加带有自定义 colors 的新选项卡? - How can I override ColorUI class of ColorEditor class and add a new Tab with custom colors? 如何在视图中修改列表项(在视图中添加/删除列表项) - How can I Modify List items in View (add/remove list items in view) 如何“覆盖”部分类中的方法? - How can I “override” a method in a partial class? 如何覆盖抽象类的属性? - How can I override an abstract class' property? 如何在列表中没有“ for”循环的情况下添加数据? - How can I add my data without “for” loop in my List? 如何添加和修改速度变量? - How can I add and modify a speed variable? 我该如何修改列表中的值 <t> ? - how can i modify a value in list<t>? 如果我不能直接修改类,如何使用元素类型的字段值从另一个列表中删除列表? - How can I remove a list from another list using a field value of the element's type if i can't modify the class directly? 如何将班级列表项添加到列表框? - How can I add class list items to listbox?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM