我正在创建一个新类,以便我可以使System.Timers.Timer类更适合我的需求。我像这样创建我的新课......
using System.Timers;
class BtElapsedEventArgs : ElapsedEventArgs
{
//My extras
}
namespace MyGreatNewTimer
{
class BetterTimer : Timer
{
}
}
现在我只想替换其他地方发生的Elapsed
事件。
private void TestTimer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
//Timer has elapsed
}
我希望它能产生以下......
private void TestTimer_Elapsed(object sender, BtElapsedEventArgs e)
{
//Timer has elapsed
}
我该怎么做呢?
答案 0 :(得分:4)
如果我理解您的问题,您可以关注How to: Publish Events that Conform to .NET Framework Guidelines (C# Programming Guide)并创建自己的Elapsed
事件并隐藏原始Epalsed
事件:
namespace MyGreatNewTimer
{
class BtElapsedEventArgs : EventArgs
{
public DateTime SignalTime { get; set; }
//Some other properties
}
class BetterTimer : Timer
{
new public event EventHandler<BtElapsedEventArgs> Elapsed;
public BetterTimer()
{
base.Elapsed += BetterTimer_Elapsed;
}
void BetterTimer_Elapsed(object sender, ElapsedEventArgs e)
{
var handler = this.Elapsed;
if(handler!=null)
{
var bte = new BtElapsedEventArgs() { SignalTime = e.SignalTime};
//Set other properties, then fire the event
handler(sender, bte);
}
}
}
}