4

I have nearly working code but OnRequest method is full of errors, I see that it's compiled code I think. Any help to make this code into human readable code?

[AsyncStateMachine(typeof(Service1.<OnRequest>d__24))]
public Task OnRequest(object sender, SessionEventArgs e)
{
    Service1.<OnRequest>d__24 <OnRequest>d__;
    <OnRequest>d__.<>4__this = this;
    <OnRequest>d__.e = e;
    <OnRequest>d__.<>t__builder = AsyncTaskMethodBuilder.Create();
    <OnRequest>d__.<>1__state = -1;
    AsyncTaskMethodBuilder <>t__builder = <OnRequest>d__.<>t__builder;
    <>t__builder.Start<Service1.<OnRequest>d__24>(ref <OnRequest>d__);
    return <OnRequest>d__.<>t__builder.Task;
}

Or I'm helpless here, I don't know what is that and I would like explanation in worst case if I can't have solution for this.

Brandon Minnick
  • 13,342
  • 15
  • 65
  • 123
Boris Dabetic
  • 113
  • 2
  • 10
  • Your generics come after your variable name; try d. – Athena Jul 20 '17 at 13:46
  • IL Spy [should support](http://community.sharpdevelop.net/blogs/danielgrunwald/archive/2012/04/16/decompiling-async-await.aspx) decompiling `async`/`await`. – GSerg Jul 20 '17 at 13:48
  • 2
    @Ares They aren't generics - they're (badly) decompiled auto generated identifiers that the decompiler doesn't properly understand. – James Thorpe Jul 20 '17 at 13:48
  • @JamesThorpe That's pretty odd... huh. – Athena Jul 20 '17 at 13:49
  • 1
    @Ares https://stackoverflow.com/a/7310497/11683 – GSerg Jul 20 '17 at 13:51
  • Thanks for the help, I tried to run ILSpy but with no luck, can't run it for some reason on Win10. What should I do here. – Boris Dabetic Jul 20 '17 at 14:12
  • @GSerg Any chance that you know why I can't run IL Spy. Not a single version. I getting that is Stopped Working – Boris Dabetic Jul 20 '17 at 14:25
  • 1
    What are you actually trying to do? – James Thorpe Jul 20 '17 at 14:25
  • @JamesThorpe To get human readable and compilable code. Because this code can not be compiled, reports an error for obvious reasons. :/ – Boris Dabetic Jul 20 '17 at 17:47
  • Those `<` and `>` characters are invalid in C#, but not for IL. The compiler uses these to make very sure there is no clash between generated variables and "real" ones. Also it is impossible to access the generated variables (without reflection anyway) – Hans Kesting Jul 21 '17 at 12:29

1 Answers1

5

The characters < and > are not valid C# for type and variable names, but they are perfectly valid in CIL code.

ILSpy doesn't "normalize" names so you get code that is not compilable, but you can just remove the special characters to fix it:

[AsyncStateMachine(typeof(Service1.OnRequestd__24))]
public Task OnRequest(object sender, SessionEventArgs e)
{
    Service1.OnRequestd__24 OnRequestd__;
    OnRequestd__.__this = this;
    OnRequestd__.e = e;
    OnRequestd__.t__builder = AsyncTaskMethodBuilder.Create();
    OnRequestd__.__state = -1;
    AsyncTaskMethodBuilder t__builder = OnRequestd__.t__builder;
    t__builder.Start<Service1.OnRequestd__24>(ref OnRequestd__);
    return OnRequestd__.t__builder.Task;
}

This compiles perfectly if you also implement the referencing types:

public class Service1
{
    public struct OnRequestd__24 : IAsyncStateMachine
    {
        public ObjectPoolAutoTest __this;
        public SessionEventArgs e;
        public AsyncTaskMethodBuilder t__builder;
        public int __state;
        public void MoveNext() => throw new NotImplementedException();

        public void SetStateMachine(IAsyncStateMachine stateMachine) => throw new NotImplementedException();
    }
}
Stefano d'Antonio
  • 5,874
  • 3
  • 32
  • 45
  • I have same extract problem but then when build, I got an error `CS0165 Use of unassigned local variable OnRequestd__` please help. – Viet Nguyen May 19 '20 at 04:34
  • @vietnguyen09 it's been a while now since I looked at the internals of the state machine, but in this context, `OnRequestd__` it's probably a struct; maybe you're getting the error because it's defined as a class. The code above compiles with a `OnRequestd__24` struct. – Stefano d'Antonio May 19 '20 at 20:54