Unexpected anonymous class registration on a C# closure
See original GitHub issueScans of non-public classes result in registrations of compiler-generated classes on C# closures. E.g.:
internal class Foo
{
public void Bar(int i)
{
async Task Baz()
{
i = i;
}
}
}
And then:
services.Scan(s => s.FromCallingAssembly().AddClasses(false));
This results in the following unexpected registration:
ServiceType = {Name = “<<Bar>g__Baz|0>d” FullName = “MyApp.Foo+<>c__DisplayClass0_0+<<Bar>g__Baz|0>d”}, ImplementationType = {Name = “<<Bar>g__Baz|0>d” FullName = “MyApp.Foo+<>c__DisplayClass0_0+<<Bar>g__Baz|0>d”}
If I derive Foo
from a new (empty) interface IFoo
and add .AsImplementedInterfaces()
to the Scan
call, I get the following unexpected registration instead:
ServiceType = {Name = “IAsyncStateMachine” FullName = “System.Runtime.CompilerServices.IAsyncStateMachine”}, ImplementationType = {Name = “<<Bar>g__Baz|0>d” FullName = “MyApp.Foo+<>c__DisplayClass0_0+<<Bar>g__Baz|0>d”}
Issue Analytics
- State:
- Created 4 years ago
- Reactions:1
- Comments:14 (8 by maintainers)
Top GitHub Comments
Ah, I see what’s going on. Roslyn’s generating a private nested state machine class inside the display class. Only the outer class has
CompilerGeneratedAttribute
on it.I’m not really sure what the best way to solve this would be.
CompilerGeneratedAttribute
if the type is nestedIAsyncStateMachine
Or maybe a combination of them? 🤔
Pushed a new version to NuGet that should hopefully address this. Let me know if it doesn’t 😅