|
14 | 14 | * along with this program; if not, see https://sonarsource.com/license/ssal/ |
15 | 15 | */ |
16 | 16 |
|
| 17 | +using System.Reflection; |
17 | 18 | using Microsoft.CodeAnalysis.CSharp; |
18 | 19 |
|
19 | 20 | namespace SonarAnalyzer.ShimLayer; |
20 | 21 |
|
21 | 22 | internal static class SyntaxNodeTypes |
22 | 23 | { |
23 | | - private static readonly ImmutableDictionary<Type, Type> Map; // Wrapper type => Latest Roslyn type |
24 | | - |
25 | | - static SyntaxNodeTypes() |
| 24 | + public static Type LatestType(Type wrapper) |
26 | 25 | { |
27 | | - var builder = ImmutableDictionary.CreateBuilder<Type, Type>(); |
28 | | - var assembly = typeof(CSharpSyntaxNode).Assembly; |
29 | | - foreach (var wrapper in typeof(SyntaxNodeTypes).Assembly.ExportedTypes) |
30 | | - { |
31 | | - if ((Load(wrapper, nameof(BaseNamespaceDeclarationSyntaxWrapper.WrappedTypeName)) ?? Load(wrapper, nameof(BaseNamespaceDeclarationSyntaxWrapper.FallbackWrappedTypeName))) is { } type) |
32 | | - { |
33 | | - builder.Add(wrapper, type); |
34 | | - } |
35 | | - } |
36 | | - Map = builder.ToImmutable(); |
| 26 | + return Load(wrapper, nameof(BaseNamespaceDeclarationSyntaxWrapper.WrappedTypeName)) ?? Load(wrapper, nameof(BaseNamespaceDeclarationSyntaxWrapper.FallbackWrappedTypeName)); |
37 | 27 |
|
38 | | - Type Load(Type wrapper, string fieldName) => |
39 | | - wrapper.GetField(fieldName) is { } field && field.GetValue(null) is string name ? assembly.GetType(name) : null; |
| 28 | + static Type Load(Type wrapper, string fieldName) => |
| 29 | + wrapper.GetField(fieldName, BindingFlags.Static | BindingFlags.Public) is { } field && field.GetValue(null) is string name |
| 30 | + ? typeof(CSharpSyntaxNode).Assembly.GetType(name) // This may need to be extended to other assemblies if needed. See TypeLoader.LoadBaseline and .LoadLatest |
| 31 | + : null; |
40 | 32 | } |
41 | | - |
42 | | - public static Type LatestType(Type wrapper) => |
43 | | - Map.TryGetValue(wrapper, out var latest) ? latest : null; |
44 | 33 | } |
0 commit comments