runtime: Compilation.GetTypeByMetadataName does not account for accessibility

The following code is potentially problematic:

https://github.com/dotnet/runtime/blob/c8a658ccadf808490b206d31d02c957f549d53a2/src/libraries/System.Text.Json/gen/Reflection/MetadataLoadContextInternal.cs#L26

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), 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.

See also https://github.com/dotnet/roslyn/issues/52399

About this issue

  • Original URL
  • State: closed
  • Created 3 years ago
  • Reactions: 1
  • Comments: 16 (15 by maintainers)

Most upvoted comments

I believe this can be closed now since #59774 was merged. @danmoseley @mkArtakMSFT I made a new issue in ASP.NET to evaluate.

I would typically use the alternative method everywhere. I can’t think of a downside of this, and it’s the approach used by dotnet/roslyn-analyzers.