用 Roslyn 为某个方法的调用者提供信息
Provide information for the caller of a certain method with Roslyn
我有一个方法有 1 个重载,我想通过 Roslyn 查明是否有人正在调用重载方法并在这种情况下显示提示。
方法如下所示:
public void Info(string message, [CallerMemberName] string memberName = "")
{
}
public void Info(string message, string secondMessage, [CallerMemberName] string memberName = "")
{
}
例如,如果有人键入:
Info("The message", secondMessage: "Second message");
我想向开发者展示一些信息。
可以用 Roslyn 做到这一点吗?
Is it possible to do this with Roslyn?
是的。您需要从语义模型中获取方法符号,然后使用 FindReferencesAsync
// Get your semantic model
var semanticModel = compilation.GetSemanticModel(tree);
//Or
var semanticModel = document.GetSemanticModelAsync();
// Get the method you want to find references to.
// You have a lot of ways to do that, but for example:
var method = doc.GetSyntaxRootAsync().
Result.DescendantNodes().
OfType<InvocationExpressionSyntax>().
First();
//Or
var method = root.DescendantNodes().
OfType<InvocationExpressionSyntax>().
First();
//Then get the symbol info of the method
var methodSymbol = semanticModel.GetSymbolInfo(method).Symbol;
// And finally
SymbolFinder.FindReferencesAsync(methodSymbol, solution).Result
我建议阅读有关 Solution\Project\Document
、SyntaxTree\Root\Node
、Compilation\SemanticModel
的内容。
一旦你理解了这一点,就可以很容易地编写出你想要的分析器。我可以在此处粘贴一个分析器示例,但您可以在网上找到更多信息(例如,查看我评论中的 link)。
视情况而定,可能仅添加[Obsolete]
:
就足够了
[Obsolete("You're probably doing it wrong, neighbour", false)]
public void Info(string message, string secondMessage,
[CallerMemberName] string memberName = "")
如果您想从您自己的代码中调用它而不出现警告:
#pragma warning disable 0618
Info("foo", "bar", "blap");
#pragma warning restore 0618
我有一个方法有 1 个重载,我想通过 Roslyn 查明是否有人正在调用重载方法并在这种情况下显示提示。
方法如下所示:
public void Info(string message, [CallerMemberName] string memberName = "")
{
}
public void Info(string message, string secondMessage, [CallerMemberName] string memberName = "")
{
}
例如,如果有人键入:
Info("The message", secondMessage: "Second message");
我想向开发者展示一些信息。
可以用 Roslyn 做到这一点吗?
Is it possible to do this with Roslyn?
是的。您需要从语义模型中获取方法符号,然后使用 FindReferencesAsync
// Get your semantic model
var semanticModel = compilation.GetSemanticModel(tree);
//Or
var semanticModel = document.GetSemanticModelAsync();
// Get the method you want to find references to.
// You have a lot of ways to do that, but for example:
var method = doc.GetSyntaxRootAsync().
Result.DescendantNodes().
OfType<InvocationExpressionSyntax>().
First();
//Or
var method = root.DescendantNodes().
OfType<InvocationExpressionSyntax>().
First();
//Then get the symbol info of the method
var methodSymbol = semanticModel.GetSymbolInfo(method).Symbol;
// And finally
SymbolFinder.FindReferencesAsync(methodSymbol, solution).Result
我建议阅读有关 Solution\Project\Document
、SyntaxTree\Root\Node
、Compilation\SemanticModel
的内容。
一旦你理解了这一点,就可以很容易地编写出你想要的分析器。我可以在此处粘贴一个分析器示例,但您可以在网上找到更多信息(例如,查看我评论中的 link)。
视情况而定,可能仅添加[Obsolete]
:
[Obsolete("You're probably doing it wrong, neighbour", false)]
public void Info(string message, string secondMessage,
[CallerMemberName] string memberName = "")
如果您想从您自己的代码中调用它而不出现警告:
#pragma warning disable 0618
Info("foo", "bar", "blap");
#pragma warning restore 0618