从 CatchDeclaration 获取符号
Get Symbol from CatchDeclaration
如何在 CatchDeclaration 中获取实例的符号信息?基本上我想获取该符号,以便稍后可以比较它以查看对该符号调用了一个方法。
基本上我有这个:catch (Exception ex) {}
我想要 "ex".
的 SymbolInfo
我得到了 catch 声明:
var catchDeclaration = catchClause.DescendantNodes().OfType<CatchDeclarationSyntax>().FirstOrDefault();
但我似乎只能从声明 (catchDeclaration.Identifier
) 中获取 SyntaxToken,它不能用于从语义模型中获取符号,因为它只需要一个 SyntaxNode。
致电semanticModel.GetDeclaredSymbol(theCatchDeclarationItself)
。要调用的方法是 here 并且是一种扩展方法,因此请确保您已在文件顶部使用 Microsoft.CodeAnalysis。
一般来说,GetSymbolInfo
用于绑定指向其他地方的变量。 GetDeclaredSymbol
用于 "get me the symbol being defined here"。
如何在 CatchDeclaration 中获取实例的符号信息?基本上我想获取该符号,以便稍后可以比较它以查看对该符号调用了一个方法。
基本上我有这个:catch (Exception ex) {}
我想要 "ex".
我得到了 catch 声明:
var catchDeclaration = catchClause.DescendantNodes().OfType<CatchDeclarationSyntax>().FirstOrDefault();
但我似乎只能从声明 (catchDeclaration.Identifier
) 中获取 SyntaxToken,它不能用于从语义模型中获取符号,因为它只需要一个 SyntaxNode。
致电semanticModel.GetDeclaredSymbol(theCatchDeclarationItself)
。要调用的方法是 here 并且是一种扩展方法,因此请确保您已在文件顶部使用 Microsoft.CodeAnalysis。
一般来说,GetSymbolInfo
用于绑定指向其他地方的变量。 GetDeclaredSymbol
用于 "get me the symbol being defined here"。