例子:
定义一个枚举,进行日志筛选
public enum Severity
{
Verbose,
Trace,
Information,
Warning,
Error,
Critical
}
把错误消息输出到控制台,并记录到txt文本。 这里采用+=的形式,进行委托注册。
委托内注册方法
static void Main(string[] args)
{
//委托方法的注册
Logger.WriteMessage += LoggingMethods.LogToConsole;
FileLogger f = new FileLogger(@"Log.txt");
Logger.LogMessage(Severity.Error, "Delegete", "错误状态");
Console.ReadKey();
}
输出到控制台
//处理错误消息
public static class Logger
{
public static Action<string> WriteMessage;
public static Severity LogLevel { get; set; } = Severity.Warning;
public static void LogMessage(Severity s, string component, string msg)
{
if (s < LogLevel)
return;
var outputMsg = $"{DateTime.Now}\t{s}\t{component}\t{msg}";
WriteMessage(outputMsg);
}
}
//输出到控制台
public static class LoggingMethods
{
public static void LogToConsole(string message)
{
Console.Error.WriteLine(message);
}
}
输出到txt
public class FileLogger
{
private readonly string logPath;
public FileLogger(string path)
{
logPath = path;
//注册
Logger.WriteMessage += LogMessage;
}
public void DetachLog() => Logger.WriteMessage -= LogMessage;
// make sure this can't throw.
private void LogMessage(string msg)
{
try
{
using (var log = File.AppendText(logPath))
{
log.WriteLine(msg);
log.Flush();
}
}
catch (Exception)
{
// Hmm. We caught an exception while
// logging. We can't really log the
// problem (since it's the log that's failing).
// So, while normally, catching an exception
// and doing nothing isn't wise, it's really the
// only reasonable option here.
}
}
}
空委托的处理方式
public static void LogMessage(string msg)
{
WriteMessage?.Invoke(msg);
}
当左操作数(本例中为 WriteMessage)为 null 时,null 条件运算符(?.)会短路,这意味着不会尝试记录消息。
不会在 System.Delegate 或 System.MulticastDelegate 的文档中列出 Invoke() 方法。 编译器将为声明的所有委托类型生成类型安全的 Invoke 方法。 在此示例中,这意味着 Invoke 只需要一个 string 参数,并且有一个无效返回类型。
总结:
那么现在可以得出这样的结论了:无返回值的委托,你给它注册多少个方法,它就执行多少个方法,而有返回值的委托,同样注册多少个方法就执行多少个方法,但返回的是最后一个方法的返回值。
2个字符长度的比较的小例子:
public delegate int Comparison<in T>(T left, T right);
private static int CompareLength(string left, string right) =>left.Length.CompareTo(right.Length);
static void Main(string[] args)
{
var a = CompareLength("234124", "234124");
Console.WriteLine(a);
Console.ReadKey();
}