cecilifier icon indicating copy to clipboard operation
cecilifier copied to clipboard

Accessing synthetic add_/remove_ methods of events declared in generic types throws a `NullReferenceException`

Open adrianoc opened this issue 9 months ago • 0 comments

When processing events defined in generic types it seems we are ignoring the fact that the declaring type is generic when registering the add/remove_Event() methods and use the non generic version of the declaring type as the parent of the method.

Later we perform a lookup to figure out whether we have a forwarded access but this time using the generic name as the parent which fails so we try to forward declare the method and that fails with a NRE.

We need to:

  1. Ensure we use the correct (generic) name when registering add/remove_{EventName} methods (potentially also with auto generated properties)
  2. Try to come up with a scenario in which there is indeed a forward reference and fix the NRE in MemberDeclarationSyntaxExtensions.Name()

Repro snippet:

using System;
class Bar<T>
{
    public event Action Event;
}

class Foo
{
    void M(Bar<int> b)
    {
        b.Event += () => {};
    }
}

adrianoc avatar May 22 '25 21:05 adrianoc