将多个事件连接到c#中的同一方法
本文关键字:方法 事件 连接 | 更新日期: 2023-09-27 18:25:36
我有三个类似的事件:
public static event EventHandler BelowLowLimit;
public static event EventHandler AboveHighLimit;
public static event EventHandler PercentBelowLowLimit;
protected virtual void OnThresholdReached()
{
EventHandler handler = null;
if (rs.Value < rs.LowLimit)
{
handler = BelowLowLimit;
}
else if (rs.Value > rs.UpperLimit)
{
handler = AboveHighLimit;
}
if (handler != null)
{
handler(this, new EventArgs());
}
}
我必须根据以下条件引发事件:
public long Rs
{
get { return rs.Value; }
set
{
if (rs.Value < rs.LowLimit)
{
OnThresholdReached();
}
else if (rs.Value > rs.UpperLimit)
{
OnThresholdReached();
}
else
{
this.rs.Value = value;
}
}
}
我想将此事件连接到引发该事件的相同方法。这样做正确吗?
请建议我怎么做?
你在看这样的东西吗?
protected virtual bool CheckForThresholds(long rsValue, long rsLowLimit, long rsUpperLimit)
{
EventHandler handler = null;
if (rsValue < rsLowLimit)
{
handler = BelowLowLimit;
}
else if (rsValue > rsUpperLimit)
{
handler = AboveHighLimit;
}
if (handler != null)
{
handler(this, new EventArgs());
return true;
}
return false;
}
public long Rs
{
get { return rs.Value; }
set
{
if (!CheckForThresholds(value, rs.LowLimit, rs.UpperLimit))
{
this.rs.Value = value;
}
}
}