在我尝试运行此代码时,Visual Studio 2015中出现CS0079编译错误:
public delegate void CostumeLOD(AvatarCustomization costume);
public event CostumeLOD OnCostumeLOD
{
add
{
CostumeLOD costumeLOD01 = this.OnCostumeLOD;
CostumeLOD costumeLOD02;
do
{
costumeLOD02 = costumeLOD01;
costumeLOD01 = Interlocked.CompareExchange(ref this.OnCostumeLOD, (CostumeLOD)Delegate.Combine(costumeLOD02, value), costumeLOD01);
}
while (costumeLOD01 != costumeLOD02);
}
remove
{
CostumeLOD costumeLOD01 = this.OnCostumeLOD;
CostumeLOD costumeLOD02;
do
{
costumeLOD02 = costumeLOD01;
costumeLOD01 = Interlocked.CompareExchange(ref this.OnCostumeLOD, (CostumeLOD)Delegate.Remove(costumeLOD02, value), costumeLOD01);
}
while (costumeLOD01 != costumeLOD02);
}
}
事件“OnCostumeLOD”只能出现在+=' or
- ='运算符
有人可以帮我找到解决此错误的方法吗?
答案 0 :(得分:0)
我不相信在这里明确地实施event
是非常必要的。您似乎试图使其成为线程安全的,但编译器提供的实现已经做了合理的工作。
也就是说,如果您希望代码工作,您需要为事件声明自己的支持字段。例如:
private CostumeLOD _onCostumeLOD;
public event CostumeLOD OnCostumeLOD
{
add
{
CostumeLOD costumeLOD01 = this._onCostumeLOD;
CostumeLOD costumeLOD02;
do
{
costumeLOD02 = costumeLOD01;
costumeLOD01 = Interlocked.CompareExchange(ref this._onCostumeLOD, (CostumeLOD)Delegate.Combine(costumeLOD02, value), costumeLOD01);
}
while (costumeLOD01 != costumeLOD02);
}
remove
{
CostumeLOD costumeLOD01 = this._onCostumeLOD;
CostumeLOD costumeLOD02;
do
{
costumeLOD02 = costumeLOD01;
costumeLOD01 = Interlocked.CompareExchange(ref this._onCostumeLOD, (CostumeLOD)Delegate.Remove(costumeLOD02, value), costumeLOD01);
}
while (costumeLOD01 != costumeLOD02);
}
}
注意:我没有声明上述内容是否真正解决了您要解决的任何线程安全问题。我只是提供如何编译的建议。