简体   繁体   English

C#:同名的普通和静态void

[英]C#: normal and static void with same name

I want to create a "log" class. 我想创建一个“日志”类。 There will be a global logger for the main things. 将有一个主要的全局记录器。 but when the network part want log something to its own logger, it should be able to. 但是当网络部分希望将某些内容记录到其自己的记录器中时,它应该可以。 My idea: a static logger instance in the logger class and a static void with the same name to log something. 我的想法是:logger类中的一个静态logger实例和一个具有相同名称的静态void来记录某些内容。 example: 例:

Logger.log("ahaha");

will log something to the main logger and 将一些东西记录到主记录器中,

new logger().log("kdsjfklsjd");

will log to a special logger. 将登录到特殊记录器。 Here the code for my class: 这是我班级的代码:

public class Logger
{
    public static Logger GlobalLogger = new Logger();

    public Logger()
    {
    }

    public void log(string message)
    {
    }

    public static void log(string message)
    {
        Logger.GlobalLogger.log(message);
    }
}

I want tell the code that when i want to Logger.GlobalLogger.log(message) it should use the nonstatic void (with the same name) 我想告诉代码,当我想要Logger.GlobalLogger.log(message)时,应使用非静态void(具有相同的名称)

You can't do it quite like that. 你不能那样做。 You'll get the compiler error: 您会得到编译器错误:

Type 'Logger' already defines a member called 'Log' with the same parameter types 类型“ Logger”已经定义了具有相同参数类型的名为“ Log”的成员

You have two main options to resolve this: 您可以通过以下两种主要方法解决此问题:

  1. Define static methods with different parameters: 用不同的参数定义静态方法:

     public class Logger { public void Log(string message) { ... } public static void Log(string message, int level) { ... } } 
  2. Follow a 'singleton' pattern: 遵循“单例”模式:

     public class Logger { public void Log(string message) { ... } private static Logger globalLogger; public static Logger GlobalLogger { get { if (globalLogger == null) { globalLogger = new Logger(); } return globalLogger; } } } 

One way to get around this compiler error is to add an interface: 解决该编译器错误的一种方法是添加一个接口:

public interface ILogger {
    void Log(string message);
}

public class Logger : ILogger {
    public static readonly ILogger GlobalLogger = new Logger();

    // explicit implementation allows the static and instance Log() to coexist peacefully
    void ILogger.Log(string message) { /* log */ }

    public static void Log(string message) { GlobalLogger.Log(message); }
}

One nice benefit of this pattern is that it is easy to provide many implementations of the ILogger interface. 这种模式的一个好处是可以轻松提供ILogger接口的许多实现。 For example, you might even consider making the Logger class static (doesn't implement ILogger ) and making a separate GlobalLogger class: 例如,您甚至可以考虑将LoggerILogger静态(不实现ILogger ),并制作一个单独的GlobalLogger类:

public static class Logger {
    public static readonly ILogger Global = new GlobalLogger();

    public static void Log(string message) { Global.Log(message); }
}

public class GlobalLogger : ILogger {
    void Log(message) { /* log */ }
}

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

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