C#8支持接口中的默认方法实现。我的想法是将一个日志记录方法注入这样的类中:
public interface ILoggable {
void Log(string message) => DoSomethingWith(message);
}
public class MyClass : ILoggable {
void MyMethod() {
Log("Using injected logging"); // COMPILER ERROR
}
}
Run Code Online (Sandbox Code Playgroud)
我收到一个编译器错误:“名称在当前上下文中不存在”
以这种方式使用默认方法实现是不可能的吗?
编辑: