Skip to content

Conversation

Rumyash
Copy link

@Rumyash Rumyash commented Dec 31, 2018

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 Rumyash force-pushed the intercept_async_method_in_generic_class branch from 134565f to 80ba0ab Compare January 1, 2019 03:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant