question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Unexpected anonymous class registration on a C# closure

See original GitHub issue

Scans 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:closed
  • Created 4 years ago
  • Reactions:1
  • Comments:14 (8 by maintainers)

github_iconTop GitHub Comments

1reaction
khellangcommented, Nov 5, 2019

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.

  1. Check outer classes for CompilerGeneratedAttribute if the type is nested
  2. Filter out types implementing IAsyncStateMachine
  3. Filter out nested private types

Or maybe a combination of them? 🤔

0reactions
khellangcommented, Feb 6, 2020

Pushed a new version to NuGet that should hopefully address this. Let me know if it doesn’t 😅

Read more comments on GitHub >

github_iconTop Results From Across the Web

The power of Anonymous functions (Closures) in PHP
Anonymous functions (someone call them Lambda functions), also known as closures (in PHP), allow the creation of functions which have no specified name....
Read more >
Self-inlining anonymous closures in C++ - Matt Might
Read on for an overview of 350 lines of C++ that includes: a DSEL for describing static anonymous closures;; a dual compile- and...
Read more >
Closures are back again!
A true closure is one that will compute all referenced variables from the enclosing scope and automatically include them in the generated code,...
Read more >
Unexpected strict mode reserved word
is being called somewhere, and my preferences are being overwritten/cleared. Is there another way to prevent reserved words being used as names?
Read more >
New features - Manual
They augment the other types introduced in PHP 5: class names, interfaces, ... Support for anonymous classes has been added via new class...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found