c# - Obtaining name of a method parameter from the corresponding argument in a method invocation in Roslyn -
i wondering if there existing logic obtain name (or other relevant information) definition of parameter in containing method/constructor signature looking @ invocation of particular method/constructor. basically, want able default name variable passed argument invocation. so, if method if defined such:
public void foo(object firstparam, object secondparam, object thirdparam) i want able second argument of following invocation
object bar = null; this.foo(null, bar, null) is expected have name "secondparam". basically, want relate argument original parameter "spot" occupies in invocation.
i asking if util methods not aware of exist within roslyn, there more complex scenarios handle, such named or optionnal arguments. solution i've come in meantime should covers cases, not (especially params, should require more specialized logic handle). here's have 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++; } } } i using declaringsyntaxreferenceson method symbol, think having names iparametersymbol suited needs enough. again, if kind of logic implemented anywhere else, i'd rather use it. if not, well, feel free tell me think of problem.
sadly don't think there public way this. see roslyn's internal determineparameter helper might help.
Comments
Post a Comment