C#如何反编译yield-return枚举器

时间:2017-03-09 01:11:55

标签: c# ienumerable decompiling yield-return

我试图反编译一些使用带有yield-return的IEnumerators的C#代码,但没有反编译器将变量重命名为有效的C#名称。我试过ILSpy,JustDecompile,dotPeek和Reflector都没有成功。

目前已编译的示例:

private IEnumerator _WaitForSecondsThenStop(float startToFadeTime, float fadeOutLength)
{
    AudioObject.<_WaitForSecondsThenStop>c__Iterator0 <_WaitForSecondsThenStop>c__Iterator = new AudioObject.<_WaitForSecondsThenStop>c__Iterator0();
    <_WaitForSecondsThenStop>c__Iterator.startToFadeTime = startToFadeTime;
    <_WaitForSecondsThenStop>c__Iterator.fadeOutLength = fadeOutLength;
    <_WaitForSecondsThenStop>c__Iterator.<$>startToFadeTime = startToFadeTime;
    <_WaitForSecondsThenStop>c__Iterator.<$>fadeOutLength = fadeOutLength;
    <_WaitForSecondsThenStop>c__Iterator.<>f__this = this;
    return <_WaitForSecondsThenStop>c__Iterator;
}

ILSpy应该able to正确地反编译枚举器,但它对我不起作用。我读过的地方可能是由于代码的一些优化导致ILSpy无法识别模式,因此可能是由此造成的。

但是,我想问的是:是否有一个C#反编译器将所有字段,变量和类重命名为有效的C#名称?我对状态自动机的疯狂很好,只要它编译为正确的C#代码。

1 个答案:

答案 0 :(得分:0)

我最终改变了这4个正则表达式的名字:

Pattern               Replacement       What it fixes
<(\w+)>c__Iterator    Iter_$1           <_WaitForSecondsThenStop>c__Iterator
<(\w+)>_              _L_$1_            <form>__2
<\$?>                 _S_               <$>startToFadeTime
\$(\w+)               _SS_$1            $current

编辑:经过一番检查后,它仍然没有完全覆盖所有内容,我修复后会更新表格。