从 Roslyn 方法调用中的相应参数获取方法参数的名称 [英] Obtaining name of a method parameter from the corresponding argument in a method invocation in Roslyn

查看:69
本文介绍了从 Roslyn 方法调用中的相应参数获取方法参数的名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道是否有一些现有的逻辑可以通过查看特定方法/构造函数的调用来获取有关参数在其包含方法/构造函数签名中的定义的名称(或任何其他相关信息).基本上,我只是希望能够获得将作为参数传递给调用的变量的默认名称.所以,如果一个方法被定义为:

I was wondering if there was some existing logic to obtain the name (or any other relevant information) about the definition of a parameter in its containing method/constructor signature by looking at an invocation of that particular method/constructor. Basically, I just want to be able to get a default name for a variable that will be passed as an argument to the invocation. So, if a method if defined as such:

public void Foo(object firstParam, object secondParam, object thirdParam)

我希望能够说以下调用的第二个参数

I would want to be able to say that the second argument of the following invocation

object bar = null;
this.Foo(null, bar, null)

应具有名称secondParam".基本上,我只想将一个参数与它在调用中占据的位置"的原始参数相关联.

is expected to have the name "secondParam". Basically, I just want to relate an argument to the original parameter whose "spot" it occupies in the invocation.

我问 Roslyn 中是否已经存在我不知道的任何 util 方法,因为有一些更复杂的场景需要处理,例如命名或可选参数.我在此期间提出的解决方案应该涵盖某些情况,但可能不是全部(尤其是 params,它应该需要一些更专业的逻辑来处理).这是我到目前为止所拥有的:

I am asking if any util methods that I am not aware of already exist within Roslyn, as there are some more complex scenarios to handle, such as named or optionnal arguments. The solution I've come up with in the meantime should covers some cases, but probably not all (especially params, which should require some more specialized logic to handle). Here's what I have so far:

    private IEnumerable<IdentifierNameSyntax> GetParameterNamesFromArgumentList(ArgumentListSyntax argumentList, SyntaxNodeAnalysisContext context)
    {
        var arguments = argumentList.Arguments;
        var parameters = argumentList.Parent.GetSymbolOrDeclaredAs<IMethodSymbol>(context)?.Parameters;

        if (parameters != null)
        {
            var index = 0;
            foreach (var parameter in parameters)
            {
                var argument = index < arguments.Count ? arguments[index] : null;

                if (argument != null && argument.NameColon == null)
                {
                    yield return SyntaxFactory.IdentifierName(parameter.Name);
                }
                else if (argument != null)
                {
                    yield return argument.NameColon.Name;
                }

                index++;
            }
        }
    }

我可以在方法符号上使用 DeclaringSyntaxReferences,但我认为仅使用 IParameterSymbol 中的名称就足以满足我的需求.同样,如果这种逻辑已经在其他任何地方实现了,我宁愿使用它.如果没有,那么请随时告诉我您对这个问题的看法.

I could be using DeclaringSyntaxReferenceson the method symbol, but I think that just having the names from the IParameterSymbol suited my needs well enough. Again, if this kind of logic is already implemented anywhere else, I'd rather use it. If not, well, feel free to tell me what you think of the problem.

推荐答案

遗憾的是,我认为没有好的公开方式可以做到这一点.请参阅 Roslyn 的内部 DetermineParameter帮助可能有帮助的事情.

Sadly I don't think there is a good public way to do this. See Roslyn's internal DetermineParameter helper for something that might help.

这篇关于从 Roslyn 方法调用中的相应参数获取方法参数的名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆