Cauldron icon indicating copy to clipboard operation
Cauldron copied to clipboard

Problem with async methods in generic class fixed.

Open Rumyash opened this issue 5 years ago • 0 comments

There are two problems with interseption async methods in generic classes. The first - the missed generic argument in the state machine's field, the second - the empty names of fields and parameters. Source code:

public class SimpleMethodInterceptorTest<T>
{
    [TestMethodInterceptor]
    public async Task<T> AsyncMethodWithParameters(int a, string b)
    {
        return await Task.Run(() => default(T));
    }
}

Generated code:

...
[AsyncStateMachine(typeof (SimpleMethodInterceptorTest<>.\u003CAsyncMethodWithParameters\u003Ed__0))]
[DebuggerStepThrough]
public Task<T> AsyncMethodWithParameters(int a, string b)
{
  if (this.\u003CAsyncMethodWithParameters\u003E_fwelbpjc2mc1 == null)
    this.\u003CAsyncMethodWithParameters\u003E_fwelbpjc2mc1 = (IMethodInterceptor) new TestMethodInterceptorAttribute();
  SimpleMethodInterceptorTest<T>.\u003CAsyncMethodWithParameters\u003Ed__0 stateMachine = new SimpleMethodInterceptorTest<T>.\u003CAsyncMethodWithParameters\u003Ed__0();
  stateMachine.\u003C\u003E4__this = this;
  stateMachine.a = a;
  stateMachine.b = b;
  stateMachine.\u003C\u003Et__builder = AsyncTaskMethodBuilder<T>.Create();
  stateMachine.\u003C\u003E1__state = -1;
  ((SimpleMethodInterceptorTest<>.\u003CAsyncMethodWithParameters\u003Ed__0) stateMachine). = a;
  ((SimpleMethodInterceptorTest<>.\u003CAsyncMethodWithParameters\u003Ed__0) stateMachine).\u003CAsyncMethodWithParameters\u003E_fwelbpjc2mc1 = this.\u003CAsyncMethodWithParameters\u003E_fwelbpjc2mc1; // the first problem
  stateMachine.\u003C\u003Et__builder.Start<SimpleMethodInterceptorTest<T>.\u003CAsyncMethodWithParameters\u003Ed__0>(ref stateMachine);
  return stateMachine.\u003C\u003Et__builder.Task;
}
...
[CompilerGenerated]
private sealed class \u003CAsyncMethodWithParameters\u003Ed__0 : IAsyncStateMachine
{
  public int \u003C\u003E1__state;
  public AsyncTaskMethodBuilder<T> \u003C\u003Et__builder;
  public int a;
  public string b;
  public SimpleMethodInterceptorTest<T> \u003C\u003E4__this;
  private T \u003C\u003Es__1;
  private TaskAwaiter<T> \u003C\u003Eu__1;
  public IMethodInterceptor \u003CAsyncMethodWithParameters\u003E_fwelbpjc2mc1;
  public int ; // the second problem

  public \u003CAsyncMethodWithParameters\u003Ed__0()
  {
    base.\u002Ector();
  }

  void IAsyncStateMachine.MoveNext()
  {
    int num1 = this.\u003C\u003E1__state;
    T s1;
    try
    {
      object[] values = new object[2]
      {
        (object) this.,
        (object) this.
      }; // the second problem
...

Rumyash avatar Dec 31 '18 11:12 Rumyash