我有一个IEnumerator,我需要在函数内部进行一些检查,如果其中一些检查失败,则需要进行一些维护,然后退出IEnumerator。但是,当我将yield break
写入内部函数时,它认为我正在尝试从该内部函数返回。
我想我可以在内部函数调用之后写private IEnumerator OuterFunction()
{
//bla bla some code
//some check:
if (!conditionA)
Fail();
//if didn't fail, continue normal code
//another check:
if (!conditionB)
Fail();
//etc....
//and here's the local function:
void Fail()
{
//some maintenance stuff I need to do
//and after the maintenance, exit out of the IEnumerator:
yield break;
//^ I want to exit out of the outer function on this line
//but the compiler thinks I'm (incorrectly) returning from the inner function Fail()
}
}
,但我想保持DRY。
{{1}}
答案 0 :(得分:0)
您需要将产量中断放在OuterFunction()中。参见What does "yield break;" do in C#?
private IEnumerator OuterFunction()
{
//bla bla some code
//some check:
if (!conditionA){
Fail();
yield break;
}
//if didn't fail, continue normal code
//another check:
if (!conditionB){
Fail();
yield break;
}
//etc....
//and here's the local function:
void Fail()
{
//some maintenance stuff I need to do
//and after the maintenance, exit out of the IEnumerator:
//^ I want to exit out of the outer function on this line
//but the compiler thinks I'm (incorrectly) returning from the inner function Fail()
}
}