2017-07-20 519 views
3

我幾乎有工作代碼,但OnRequest方法是充滿錯誤,我看到它是編譯代碼,我認爲。任何幫助將此代碼編入人類可讀代碼?C#AsyncStateMachine反編譯

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

或者我在這裏很無奈,我不知道那是什麼,我想在最壞的情況下解釋,如果我不能解決這個問題。

+0

您的泛型出現在變量名後面;嘗試d 。 – Ares

+0

IL Spy [should support](http://community.sharpdevelop.net/blogs/danielgrunwald/archive/2012/04/16/decompiling-async-await.aspx)反編譯'async' /'await'。 – GSerg

+2

@Ares它們不是泛型 - 它們(非常)反編譯自動生成的標識符,反編譯器不能正確理解。 –

回答

2

字符<>對於類型和變量名稱無效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(); 
    } 
}