C#AsyncStateMachine反编译

时间:2017-07-20 13:36:48

标签: c# async-await

我有近乎工作的代码,但OnRequest方法充满了错误,我认为它是我编写的代码。有什么帮助使这段代码成为人类可读的代码吗?

[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;
}

或者我在这里无助,我不知道那是什么,我想在最坏的情况下解释如果我无法解决这个问题。

1 个答案:

答案 0 :(得分:3)

字符<>对于类型和变量名称无效C#,但它们在CIL代码中完全有效。

ILSpy 没有“规范化”名称,因此您获得的代码不可编译,但您只需删除特殊字符即可修复它:

[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;
}

如果您还实现了引用类型,那么这将完美编译:

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();
    }
}