Compilation.GetTypeByMetadataName does not account for accessibility
- Dominant language
- C#
- Stars
- 38.4k
- Forks
- 10.9k
- Avg merge
- 2d 6h
- Merged PRs (30d)
- 290
Description
Copied from https://github.com/dotnet/runtime/issues/57349, cc @sharwell
The following code is potentially problematic:
https://github.com/dotnet/aspnetcore/search?q=GetTypeByMetadataName
If _any_ assembly referenced by the compilation defines a second copy of a type as `internal` (e.g. how Microsoft.CodeAnalysis defines its own copy of the [nullable attributes](https://github.com/dotnet/roslyn/blob/main/src/Compilers/Core/Portable/InternalUtilities/NullableAttributes.cs)), `GetTypeByMetadataName` will return null for that type even if only one copy is accessible to the compilation. The problem occurred enough times that dotnet/roslyn-analyzers banned direct calls to `Compilation.GetTypeByMetadataName` outright and provided an alternative that [falls back to an accessibility-aware check](https://github.com/dotnet/roslyn-analyzers/blob/d0663cca11722045b9aeabb5d0d5335042084e13/src/Utilities/Compiler/WellKnownTypeProvider.cs#L81-L91).
See also https://github.com/dotnet/roslyn/issues/52399
Contributor guide
Assessment
This issue has not been assessed yet.