简体   繁体   English

如何覆盖List.Add方法?

[英]How to override List.Add method?

Currently I have a error logging class like so: 目前我有一个错误记录类,如下所示:

public class Log
{
    public enum LogTypes
    {
        Info = 1,
        Error = 2,
        Warning = 3
    }


    public string Message { get; set; }
    public LogTypes LogType { get; set; }


    public Log(string Message, LogTypes LogType)
    {
        this.Message = Message;
        this.LogType = LogType;
    }

I have this initialiser for a new list: 我有一个新列表的初始化器:

List<Log> LogList = new List<Log>();

How can I use LogList.Add(Message, LogType) instead of LogList.Add(new Log(Message, LogType)); 如何使用LogList.Add(Message, LogType)代替LogList.Add(new Log(Message, LogType)); ?

I know it is a minor change but I am still learning C# and am curious. 我知道这是一个小小的改变,但我仍在学习C#,我很好奇。 Thanks! 谢谢!

Firstly, I wouldn't do this. 首先,我不会这样做。 It's not what anyone using a List<Log> would expect. 并不是任何人使用List<Log>所期望的。 Rather than exposing a plain List<Log> , consider creating a Logger class or something similar which contains a List<Log> and exposes a Log(string message, LogType type) method, but doesn't actually expose the List<Log> . 不要暴露普通的List<Log> ,而应考虑创建一个包含 List<Log>Logger类或类似的东西,并公开Log(string message, LogType type)方法,但实际上不公开List<Log>

If you really want to be able to call Add(message, type) directly on the list, there are two options: 如果您真的希望能够直接在列表上调用Add(message, type) ,有两个选项:

  • Create a new class derived from List<Log> : 创建从List<Log>派生的新类:

     public LogList : List<Log> { public void Add(string message, LogType type) { Add(new Log(message, type)); } } 

    Note that this is overloading (adding a new method signature but with the same name, Add ), not overriding (providing new behaviour for an existing signature method for a virtual method) - and you'll need to create an instance of LogList rather than List<Log> : 请注意,这是重载 (添加新方法签名但具有相同名称, Add ),而不是覆盖 (为virtual方法的现有签名方法提供新行为) - 并且您需要创建LogList的实例而不是List<Log>

     LogList list = new LogList(); list.Add(message, type); 
  • Add an extension method to List<Log> , which will effectively add that method to all List<Log> instances. List<Log>添加扩展方法,这将有效地将该方法添加到所有 List<Log>实例。

     public static LogListExtensions { public static void Add(this Log<List> list, string message, LogType type) { list.Add(new Log(message, type)); } } 

As an aside, I'd probably also remove the setters from your Log type - why would you need to be able to change the message or type after construction? 另外,我可能还会从您的Log类型中删除setter - 为什么您需要能够在构建后更改消息或类型?

You can create your own class derived from List and define new Add method for your needs, eg: 您可以创建自己的List派生类,并根据需要定义新的Add方法,例如:

public class MyListClass : List<Log>
{
    public void Add(string message, Log.LogTypes logType)
    {
        this.Add(new Log(message, logType));
    }
}

It is not going make any difference apart from saving a second or two of typing. 除了保存第二或第二次打字之外,它没有任何区别。 Having said that you can always implement your own list class and use that. 说过你总是可以实现自己的列表类并使用它。 Or you could make use of extension methods. 或者您可以使用扩展方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM