编译器警告 CS0067:从未使用过该事件
Compiler Warning CS0067 : The event is never used
我有一个正在使用的事件,所以我不太明白这个警告的真正含义。有人可以澄清一下吗?
public abstract class Actor<T> : Visual<T> where T : ActorDescription
{
#region Events
/// <summary>
/// Event occurs when the actor is dead
/// </summary>
public event Action Dead;
#endregion
/// <summary>
/// Take damage if the actor hasn't taken damage within a time limit
/// </summary>
/// <param name="damage">amount of damage</param>
public void TakeDamage(int damage)
{
if (damage > 0 && Time.time > m_LastHitTimer + m_DamageHitDelay)
{
m_CurrentHealth -= damage;
if (m_CurrentHealth <= 0)
{
m_CurrentHealth = 0;
if (Dead != null)
Dead();
}
else
StartCoroutine(TakeDamageOnSprite());
m_LastHitTimer = Time.time;
}
}
在我的另一个 class 中,我注册和注销了活动:
if (m_Player != null)
m_Player.Dead += OnPlayerDead;
if (m_Player != null)
m_Player.Dead -= OnPlayerDead;
由于 class Actor<T>
是抽象的,并且 Actor<T>
中没有代码引发事件,您可以使事件抽象:
public abstract event Action Dead;
然后在继承自 Actor<T>
的子 class(es) 中覆盖事件:
public override event Action Dead;
如果 subclass 实际上没有引发事件,那么您可以通过给事件空 add
和 remove
方法来抑制警告(参见 this blog post).
public override event Action Dead
{
add { }
remove { }
}
真正的问题是我使用的是 Unity 和 Mono 2.6,但它仍然是一个错误。因此,我尝试使用 Blorgbeard 所说的建议;它适用于 Visual Studio 但不适用于编译器。
所以有两种解决方案,围绕事件抛出#pragma warning disable,或者传递通用类型。
像这样:
public abstract class Actor<T> : Visual<T> where T : ActorDescription
{
#region Events
/// <summary>
/// Event occurs when the actor is dead
/// </summary>
public event Action<Actor<T>> Dead;
#endregion
...
private void SomeFunc()
{
if (Dead != null)
Dead();
}
}
我有一个正在使用的事件,所以我不太明白这个警告的真正含义。有人可以澄清一下吗?
public abstract class Actor<T> : Visual<T> where T : ActorDescription
{
#region Events
/// <summary>
/// Event occurs when the actor is dead
/// </summary>
public event Action Dead;
#endregion
/// <summary>
/// Take damage if the actor hasn't taken damage within a time limit
/// </summary>
/// <param name="damage">amount of damage</param>
public void TakeDamage(int damage)
{
if (damage > 0 && Time.time > m_LastHitTimer + m_DamageHitDelay)
{
m_CurrentHealth -= damage;
if (m_CurrentHealth <= 0)
{
m_CurrentHealth = 0;
if (Dead != null)
Dead();
}
else
StartCoroutine(TakeDamageOnSprite());
m_LastHitTimer = Time.time;
}
}
在我的另一个 class 中,我注册和注销了活动:
if (m_Player != null)
m_Player.Dead += OnPlayerDead;
if (m_Player != null)
m_Player.Dead -= OnPlayerDead;
由于 class Actor<T>
是抽象的,并且 Actor<T>
中没有代码引发事件,您可以使事件抽象:
public abstract event Action Dead;
然后在继承自 Actor<T>
的子 class(es) 中覆盖事件:
public override event Action Dead;
如果 subclass 实际上没有引发事件,那么您可以通过给事件空 add
和 remove
方法来抑制警告(参见 this blog post).
public override event Action Dead
{
add { }
remove { }
}
真正的问题是我使用的是 Unity 和 Mono 2.6,但它仍然是一个错误。因此,我尝试使用 Blorgbeard 所说的建议;它适用于 Visual Studio 但不适用于编译器。
所以有两种解决方案,围绕事件抛出#pragma warning disable,或者传递通用类型。
像这样:
public abstract class Actor<T> : Visual<T> where T : ActorDescription
{
#region Events
/// <summary>
/// Event occurs when the actor is dead
/// </summary>
public event Action<Actor<T>> Dead;
#endregion
...
private void SomeFunc()
{
if (Dead != null)
Dead();
}
}