Get Symbol from CatchDeclaration

六眼飞鱼酱① 提交于 2019-12-13 01:05:57

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!