Skip to content

Instantly share code, notes, and snippets.

@AdamSpeight2008
Last active May 17, 2018 09:28
Show Gist options
  • Save AdamSpeight2008/7ae44e9321c46b424e211aa7b446b4e8 to your computer and use it in GitHub Desktop.
Save AdamSpeight2008/7ae44e9321c46b424e211aa7b446b4e8 to your computer and use it in GitHub Desktop.
Extensions via Extended TypeOf Expressions
' 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 AndAlso AnonymousObjectCreationExpressionSyntax AndAlso SimpleImportsClauseSyntax AndAlso
CatchStatementSyntax AndAlso CollectionRangeVariableSyntax AndAlso EnumBlockSyntax AndAlso
EnumMemberDeclarationSyntax AndAlso EnumStatementSyntax AndAlso EventBlockSyntax AndAlso
ExpressionRangeVariableSyntax AndAlso ForEachStatementSyntax AndAlso FieldInitializerSyntax AndAlso
ForStatementSyntax AndAlso LabelStatementSyntax AndAlso MethodBaseSyntax AndAlso
MethodBlockSyntax AndAlso ModifiedIdentifierSyntax AndAlso NamespaceBlockSyntax AndAlso
NamespaceStatementSyntax AndAlso ParameterSyntax AndAlso PropertyBlockSyntax AndAlso
PropertyStatementSyntax AndAlso TypeBlockSyntax AndAlso TypeParameterSyntax AndAlso
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