-
-
Notifications
You must be signed in to change notification settings - Fork 294
Expand file tree
/
Copy pathFullyQualifiedNameRewriter.cs
More file actions
33 lines (26 loc) · 1.12 KB
/
FullyQualifiedNameRewriter.cs
File metadata and controls
33 lines (26 loc) · 1.12 KB
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
31
32
33
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Syntax;
namespace MemoryPack.Generator;
internal class FullyQualifiedNameRewriter : CSharpSyntaxRewriter
{
private readonly SemanticModel _semanticModel;
public FullyQualifiedNameRewriter(SemanticModel semanticModel)
{
_semanticModel = semanticModel;
}
public override SyntaxNode? VisitObjectCreationExpression(ObjectCreationExpressionSyntax node)
{
var typeInfo = _semanticModel.GetTypeInfo(node.Type);
var typeSymbol = typeInfo.Type ?? _semanticModel.GetSymbolInfo(node.Type).Symbol as ITypeSymbol;
if (typeSymbol != null)
{
var fullyQualifiedName = typeSymbol.ToDisplayString(SymbolDisplayFormat.FullyQualifiedFormat);
var newTypeSyntax = SyntaxFactory.ParseTypeName(fullyQualifiedName)
.WithLeadingTrivia(node.Type.GetLeadingTrivia())
.WithTrailingTrivia(node.Type.GetTrailingTrivia());
node = node.WithType(newTypeSyntax);
}
return base.VisitObjectCreationExpression(node);
}
}