Repository: dotnet/aspnetcore
File: src/Shared/RoslynUtils/WellKnownTypes.cs (C#)

Code before:
    private static void AssertEnumAndTableInSync()
    {
        for (var i = 0; i < WellKnownTypeData.WellKnownTypeNames.Length; i++)
        {
            var name = WellKnownTypeData.WellKnownTypeNames[i];
            var typeId = (WellKnownTypeData.WellKnownType)i;

            var typeIdName = typeId.ToString().Replace("__", "+").Replace('_', '.');

            var separator = name.IndexOf('`');
            if (separator >= 0)
            {
                // Ignore type parameter qualifier for generic types.
                name = name.Substring(0, separator);
                typeIdName = typeIdName.Substring(0, separator);
            }

            Debug.Assert(name == typeIdName, $"Enum name ({typeIdName}) and type name ({name}) must match at {i}");
        }
    }

    private WellKnownTypes(Compilation compilation)
    {
        _lazyWellKnownTypes = new INamedTypeSymbol?[WellKnownTypeData.WellKnownTypeNames.Length];
        _compilation = compilation;
        _missingTypeSymbol = compilation.GetTypeByMetadataName(typeof(MissingType).FullName!)!;
    }

    public INamedTypeSymbol Get(SpecialType type)
    {
        return _compilation.GetSpecialType(type);
    }

    public INamedTypeSymbol Get(WellKnownTypeData.WellKnownType type)
    {
        var index = (int)type;
        var symbol = _lazyWellKnownTypes[index];
        if (symbol is not null)
        {
            return symbol;
        }

        // Symbol hasn't been added to the cache yet.
        // Resolve symbol from name, cache, and return.
        return GetAndCache(index);
    }

    private INamedTypeSymbol GetAndCache(int index)
    {
        var result = GetTypeByMetadataNameInTargetAssembly(WellKnownTypeData.WellKnownTypeNames[index]) ?? _missingTypeSymbol;
        Interlocked.CompareExchange(ref _lazyWellKnownTypes[index], result, null);

Review comment (suggestion):
I wonder if we should consider reusing SpecialType.None as the fallback in this case as Roslyn does in their WellKnownType implementation ([ref](https://github.com/dotnet/roslyn/blob/322037893f72b11ce30cb14bb1727798f0a7eb4a/src/Compilers/Core/Portable/WellKnownTypes.cs#L19)). That let's us reuse an existing concept instead of defining out own marker type.

Code after:
    {
        for (var i = 0; i < WellKnownTypeData.WellKnownTypeNames.Length; i++)
        {
            var name = WellKnownTypeData.WellKnownTypeNames[i];
            var typeId = (WellKnownTypeData.WellKnownType)i;

            var typeIdName = typeId.ToString().Replace("__", "+").Replace('_', '.');

            var separator = name.IndexOf('`');
            if (separator >= 0)
            {
                // Ignore type parameter qualifier for generic types.
                name = name.Substring(0, separator);
                typeIdName = typeIdName.Substring(0, separator);
            }

            Debug.Assert(name == typeIdName, $"Enum name ({typeIdName}) and type name ({name}) must match at {i}");
        }
    }

    private WellKnownTypes(Compilation compilation)
    {
        _lazyWellKnownTypes = new INamedTypeSymbol?[WellKnownTypeData.WellKnownTypeNames.Length];
        _compilation = compilation;
    }

    public INamedTypeSymbol Get(SpecialType type)
    {
        return _compilation.GetSpecialType(type);
    }

    /// <summary>
    /// Returns the type symbol for the specified well-known type, or throws if the type cannot be found.
    /// </summary>
    public INamedTypeSymbol Get(WellKnownTypeData.WellKnownType type)
    {
        return Get(type, throwOnNotFound: true);
    }

    /// <summary>
    /// Returns the type symbol for the specified well-known type, or a special marker type symbol if the type cannot be found.
    /// </summary>
    /// <remarks>
    /// We use a special marker type for cases where some types can be legitimately missing.
    /// E.g. The Microsoft.Extensions.Validation source generator checks against some types
    /// from the shared framework which are missing in Blazor WebAssembly SDK projects.
    /// </remarks>
    public INamedTypeSymbol GetOptional(WellKnownTypeData.WellKnownType type)
    {
        return Get(type, throwOnNotFound: false);
    }