CGC_Codes

ConGEN

Feb 21st, 2017
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.51 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using Microsoft.CodeAnalysis.CSharp;
  5. using Microsoft.CodeAnalysis.CSharp.Syntax;
  6. using Testura.Code.Generators.Common;
  7. using Testura.Code.Models;
  8. using Attribute = Testura.Code.Models.Attribute;
  9.  
  10. namespace Testura.Code.Generators.Class
  11. {
  12.     public static class ConstructorGenerator
  13.     {
  14.        
  15.         public static ConstructorDeclarationSyntax Create(
  16.             string className,
  17.             BlockSyntax body,
  18.             IEnumerable<Parameter> parameters = null,
  19.             IEnumerable<Modifiers> modifiers = null,
  20.             IEnumerable<Attribute> attributes = null)
  21.         {
  22.             if (className == null)
  23.             {
  24.                 throw new ArgumentNullException(nameof(className));
  25.             }
  26.  
  27.             var constructor = SyntaxFactory.ConstructorDeclaration(SyntaxFactory.Identifier(className))
  28.                         .WithBody(body);
  29.             if (parameters != null)
  30.             {
  31.                 constructor = constructor.WithParameterList(ParameterGenerator.Create(parameters.ToArray()));
  32.             }
  33.  
  34.             if (attributes != null)
  35.             {
  36.                 constructor = constructor.WithAttributeLists(AttributeGenerator.Create(attributes.ToArray()));
  37.             }
  38.  
  39.             if (modifiers != null)
  40.             {
  41.                 constructor = constructor.WithModifiers(ModifierGenerator.Create(modifiers.ToArray()));
  42.             }
  43.  
  44.             return constructor;
  45.         }
  46.     }
  47. }
Advertisement
Add Comment
Please, Sign In to add comment