简体   繁体   中英

C# how to decompile yield-return enumerators

I am trying to decompile some C# code that uses IEnumerators with yield-return, but no decompiler bothers to rename the variables to valid C# names. I have tried ILSpy, JustDecompile, dotPeek and Reflector with no success.

Decompiled example so far:

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 is supposedly able to decompile enumerators properly, however it doesn't work for me. Somewhere I've read it might be caused by some optimizations of the code that make it impossible for ILSpy to recognize the patters, so it may be caused by that.

However, what I want to ask is this: Is there a C# decompiler that will rename all the field, variables and classes to valid C# names? I'm fine with the state automaton madness, as long as it compiles as a proper C# code.

I ended up changing the names with these 4 regexes:

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

EDIT: after some inspection, it still doesn't quite cover everything, I'll update the table once I fix it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM