Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Invalid class name from reflection .NET 6

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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?

>Solution :

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading