title: Adding an attribute to method parameter with Roslyn codefix
body: <p>I'm making an analyzer with Roslyn to add the postsharp [NotNull] attribute to method parameters. </p>
  
  <pre><code>private async Task&lt;Document&gt; MakeNotNullAsync(Document document, MethodDeclarationSyntax method, CancellationToken cancellationToken, string paramName)
      {
  
          var parameters = method.ChildNodes().OfType&lt;ParameterListSyntax&gt;().First();
          var param = parameters.ChildNodes().OfType&lt;ParameterSyntax&gt;().First() as SyntaxNode;
  
  
  
          NameSyntax name = SyntaxFactory.ParseName("NotNull");
          AttributeSyntax attribute = SyntaxFactory.Attribute(name, null);
          Collection&lt;AttributeSyntax&gt; list = new Collection&lt;AttributeSyntax&gt;();
          list.Add(attribute);
          var separatedlist = SyntaxFactory.SeparatedList(list);
          var newLiteral = SyntaxFactory.AttributeList(separatedlist);
          Collection&lt;SyntaxNode&gt; synlist = new Collection&lt;SyntaxNode&gt;();
          synlist.Add(newLiteral);
  
          var root = await document.GetSyntaxRootAsync();
          var newRoot = root.InsertNodesBefore(param, synlist);
          var newDocument = document.WithSyntaxRoot(newRoot);
          return newDocument;
  </code></pre>
  
  <p>Here is what I have so far (and there is definitely an easier way to do this) but I am getting a 'System.InvalidCastException' when it tries to do root.InsertNodesBefore(). Is there a better way to do this?</p>