Skip to content

Instantly share code, notes, and snippets.

@AdamSpeight2008
Created May 17, 2018 09:32
Show Gist options
  • Save AdamSpeight2008/5c7dc0253cc1185cb536237f951ffb4e to your computer and use it in GitHub Desktop.
Save AdamSpeight2008/5c7dc0253cc1185cb536237f951ffb4e to your computer and use it in GitHub Desktop.
Using TypeOf Expressions with TypeListSyntax
' Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
Imports System
Imports System.Runtime.CompilerServices
Imports System.Threading
Imports Microsoft.CodeAnalysis
Imports Microsoft.CodeAnalysis.VisualBasic
Imports Microsoft.CodeAnalysis.VisualBasic.Symbols
Imports Microsoft.CodeAnalysis.VisualBasic.Syntax
' This is deliberately declared in the global namespace so that it will always be discoverable (regardless of Imports).
Friend Module Extensions
''' <summary>
''' This method is provided as a convenience for testing the SemanticModel.GetDeclaredSymbol implementation.
''' </summary>
''' <param name="node">This parameter will be type checked, and a NotSupportedException will be thrown if the type is not currently supported by an overload of GetDeclaredSymbol.</param>
<Extension()>
Friend Function GetDeclaredSymbolFromSyntaxNode(model As SemanticModel, node As SyntaxNode, Optional cancellationToken As CancellationToken = Nothing) As Symbol
If (Typeof node IsNot
{AggregationRangeVariableSyntax, AnonymousObjectCreationExpressionSyntax, SimpleImportsClauseSyntax,
CatchStatementSyntax, CollectionRangeVariableSyntax, EnumBlockSyntax,
EnumMemberDeclarationSyntax, EnumStatementSyntax, EventBlockSyntax,
xpressionRangeVariableSyntax, ForEachStatementSyntax, FieldInitializerSyntax,
ForStatementSyntax, LabelStatementSyntax, MethodBaseSyntax,
MethodBlockSyntax, ModifiedIdentifierSyntax, NamespaceBlockSyntax,
NamespaceStatementSyntax, ParameterSyntax, PropertyBlockSyntax,
PropertyStatementSyntax, TypeBlockSyntax, TypeParameterSyntax,
TypeStatementSyntax } ) _
Then
Throw New NotSupportedException("This node type is not supported.")
End If
Return DirectCast(model.GetDeclaredSymbol(node, cancellationToken), Symbol)
End Function
End Module
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment