问题
How do I get the symbol info of the instance in a CatchDeclaration? Basically I want to get the symbol so that I can compare it later to see that a method was called on that symbol.
Basically I have this: catch (Exception ex) {}
and I want the SymbolInfo for "ex".
I get the catch declaration with:
var catchDeclaration = catchClause.DescendantNodes().OfType<CatchDeclarationSyntax>().FirstOrDefault();
But I seem to only be able to get the SyntaxToken from the declaration (catchDeclaration.Identifier
) which cannot be used to get a symbol from the semantic model since that only takes a SyntaxNode.
回答1:
Call semanticModel.GetDeclaredSymbol(theCatchDeclarationItself)
. The method to call is here and is an extension method, so make sure you've got using Microsoft.CodeAnalysis at the top of your file.
In general, GetSymbolInfo
is used when you're binding a variable that points somewhere else. GetDeclaredSymbol
is used for "get me the symbol being defined here".
来源:https://stackoverflow.com/questions/33275697/get-symbol-from-catchdeclaration