gpt4 book ai didi

c# - 手动调用EventHandler时出现无意义的错误消息

转载 作者:行者123 更新时间:2023-12-03 03:43:09 26 4
gpt4 key购买 nike

如果我有一个具有以下签名的事件处理程序:

public static void ProcessStuff(object sender, ElapsedEventArgs e)

如果我尝试像这样手动调用 EventHandler:

ProcessStuff(this, System.Timers.ElapsedEventArgs.Empty);

为什么我收到错误消息:

Argument 2: cannot convert from 'System.EventArgs' to 'System.Timers.ElapsedEventArgs'

编辑:如果我将手动调用设置为:

,我也会收到相同的错误消息:
ProcessStuff(this, System.EventArgs.Empty);

最佳答案

如果我们看一下 https://referencesource.microsoft.com/#System/services/timers/system/timers/ElapsedEventArgs.cs,fa59a445f56b7851

我们会看到System.Timers.ElapsedEventArgs 不重新声明 Empty :

// No "Empty" (re-)declaration here
public class ElapsedEventArgs : EventArgs {
private DateTime signalTime;

internal ElapsedEventArgs(int low, int high) {
long fileTime = (long)((((ulong)high) << 32) | (((ulong)low) & 0xffffffff));
this.signalTime = DateTime.FromFileTime(fileTime);
}

public DateTime SignalTime {
get {
return this.signalTime;
}
}
}

这就是为什么当我们调用System.Timers.ElapsedEventArgs.Empty时我们实际上打电话 System.EventArgs.Empty其类型为 System.EventArgs :

Console.Write(System.Timers.ElapsedEventArgs.Empty.GetType().Name);

最后,System.EventArgs 中没有隐式转换System.Timers.ElapsedEventArgs

关于c# - 手动调用EventHandler时出现无意义的错误消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55162084/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com