0

I am trying to get the parameters list of a class method using Roslyn and noticed a strange behavior that Roslyn considers and returns a Lambda parameter used inside the body of the method as as one of the parameters of the method, which cause an error in my code. Why does Roslyn consider a lambda parameter as of the method's parameters?

Here is the code:

var paramDeclaratons = memmeth.DescendantNodes().OfType<ParameterSyntax>();
foreach (var mempara in paramDeclaratons)
{                            
    String paramType = mempara.Type.ToFullString().Trim(); //Here it crashes with System.NullReferenceException because Lambda returns no type!

The code which is parsed:

public void Method1(RequestId requestId)
{
     ...
     var packetsToKeep = this.queuedPackets.Where(p => p.RequestId != requestId)

p is returned as one of the parameters of Method1 with no type

Supernova
  • 137
  • 9
  • Crashes how? Which exception is thrown? – Lasse V. Karlsen Jun 29 '17 at 07:54
  • Throws System.NullReferenceException – Supernova Jun 29 '17 at 07:56
  • ```mempara.Type.ToFullString()?.Trim();``` I would suggest you use nullpropagation. – Headhunter Xamd Jun 29 '17 at 07:58
  • 1
    Possible duplicate of [What is a NullReferenceException, and how do I fix it?](https://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – Liam Jun 29 '17 at 07:58
  • 2
    Because `p => p.RequestId != requestId` is a lambda, and you asked for *all* descendant nodes of the method... – Lucas Trzesniewski Jun 29 '17 at 07:58
  • Roslyn is not claiming that `p` is a parameter to your method, it is claiming that `p` is a parameter to something that is a descendant of your `memmeth`. That's what you asked for. As to why `.Type` is `null` in this case I don't know, you will have to check the documentation. – Lasse V. Karlsen Jun 29 '17 at 07:59
  • I was wondering that maybe is not the right type then for only getting method parameters. Maybe there is another type I should use here for that purpose? – Supernova Jun 29 '17 at 08:02
  • "Sometimes it is difficult or impossible for the compiler to infer the input types." So you can always give the parameter a type. This is explained here: https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/statements-expressions-operators/lambda-expressions – Headhunter Xamd Jun 29 '17 at 08:05
  • Guys, I edited the question and removed the part I wrote on how to handle the NullReferenceException error. I was more curious about if I should use another type instead of to only get method's parameters and noticed many people referred me to general solutions for handling NullReferenceException and down-voted my question. To avoid this confusion I removed that part. – Supernova Jun 29 '17 at 08:07

1 Answers1

0

Assuming memmeth is a MethodDeclarationSyntax, then what you want is to access its ParameterList.Parameters:

var paramDeclaratons = memmeth.ParameterList.Parameters;
svick
  • 236,525
  • 50
  • 385
  • 514