0
VB에서 Roslyn을 사용하여 형식 유추를 사용하여 선언 된 변수에 대해 ITypeSymbol에 액세스하려고하지만 올바른 노드를 반환하는 데 어려움을 겪고 있습니다. ITypeSymbol이 변수가 참조 유형인지 여부를 식별 할 수있게하려고합니다. 비교해 보면 C#에서는 상대적으로 쉽습니다. 이 경우 간단히 node.Declaration.Type
을 사용할 수 있지만 VB 선언 기에서 사용할 수는 없습니다. 다음 예에서VB에서 Roslyn을 사용하여 추측 된 변수에 대한 TypeSymbol을 가져옵니다.
, 나는 b
선언에 대한 AsClause
에서 TypeSymbol에 액세스 할 수있어, 그러나 그것은 a
변수에 사용되는 추론 유형 널 :
Imports System.IO
Module Module1
Sub Main()
Dim code = "
Class C
Shared Sub Main()
Dim a = """"
Dim b As String = """"
End Sub
End Class"
Dim tree = SyntaxFactory.ParseSyntaxTree(code)
Dim compilation = VisualBasicCompilation.Create("test", {tree}, {MetadataReference.CreateFromAssembly(GetType(Object).Assembly)})
Dim result = compilation.Emit(New MemoryStream)
Dim semanticModel = compilation.GetSemanticModel(tree)
Dim localNodes = tree.GetRoot().DescendantNodes.OfType(Of LocalDeclarationStatementSyntax)
For Each node In localNodes
Dim localSym = semanticModel.GetDeclaredSymbol(node.Declarators.Single.Names.Single)
Trace.WriteLine(localSym.ToDisplayString())
' TODO: Figure how to get the typeinfo from inferred type
Dim symbol = semanticModel.GetTypeInfo(node) ' Is Nothing
Dim variableType = node.Declarators.First.AsClause?.Type ' This is null for inferred types
If variableType IsNot Nothing Then
Dim typeSymbol = SemanticModel.GetTypeInfo(variableType).ConvertedType
If typeSymbol.IsReferenceType AndAlso typeSymbol.SpecialType <> SpecialType.System_String Then
' Real processing goes here
End If
End If
Next
End Sub
End Module
감사합니다. 이'Type'은'AsClause.Type'과 같은 타입이 아니라'semanticModel.GetTypeInfo (variable.AsClause.Type) .ConvertedType'입니다. 마침내 그 고비를 넘겼습니다. Roslyn에서 배울 점이 많습니다. –