forked from dotnet/roslyn-analyzers
/
CompilationExtensions.cs
30 lines (28 loc) · 1.56 KB
/
CompilationExtensions.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using Microsoft.CodeAnalysis;
namespace Analyzer.Utilities.Extensions
{
/// <summary>
/// Provides extensions to <see cref="Compilation"/>.
/// </summary>
internal static class CompilationExtensions
{
/// <summary>
/// Gets a type by its full type name and cache it at the compilation level.
/// </summary>
/// <param name="compilation">The compilation.</param>
/// <param name="fullTypeName">Namespace + type name, e.g. "System.Exception".</param>
/// <returns>The <see cref="INamedTypeSymbol"/> if found, null otherwise.</returns>
internal static INamedTypeSymbol? GetOrCreateTypeByMetadataName(this Compilation compilation, string fullTypeName) =>
WellKnownTypeProvider.GetOrCreate(compilation).GetOrCreateTypeByMetadataName(fullTypeName);
/// <summary>
/// Gets a type by its full type name and cache it at the compilation level.
/// </summary>
/// <param name="compilation">The compilation.</param>
/// <param name="fullTypeName">Namespace + type name, e.g. "System.Exception".</param>
/// <returns>The <see cref="INamedTypeSymbol"/> if found, null otherwise.</returns>
internal static bool TryGetOrCreateTypeByMetadataName(this Compilation compilation, string fullTypeName, [NotNullWhen(returnValue: true)] out INamedTypeSymbol? namedTypeSymbol) =>
WellKnownTypeProvider.GetOrCreate(compilation).TryGetOrCreateTypeByMetadataName(fullTypeName, out namedTypeSymbol);
}
}