Home > other >  Invalid class name from reflection .NET 6
Invalid class name from reflection .NET 6

Time:01-30

I'm trying to get all classes from the namespace:

var commands = Assembly.GetExecutingAssembly().GetTypes()
            .Where(t => String.Equals(t.Namespace, nameSpace, StringComparison.Ordinal))
            .ToList();

I have only one class in this namespace:

enter image description here

But, in commands variable I have 2 classes:
enter image description here

I don't understand why it breaks. I also solved this trouble by adding
!t.FullName!.Contains("<>") in where statement of commands, but I don't think that it's perfect solution.
Can somebody explain me why reflection breaks?

CodePudding user response:

Nothing breaks, that's how classes generated by the compiler to run your code look like, specifically lambda functions, yieldable enumerable state machines and async/await state machines. It uses characters that C# explicitly doesn't support as part of identifiers (<>) on purpose, so there's never any clash, even though the CLR supports them.

  •  Tags:  
  • Related