コード例 #1
0
        public CSharpSyntaxNode Convert(ClassDeclaration node)
        {
            ClassDeclarationSyntax csClass = SyntaxFactory
                                             .ClassDeclaration(node.Name.Text)
                                             .AddModifiers(node.Modifiers.ToCsNodes <SyntaxToken>())
                                             .AddMembers(node.Members.ToCsNodes <MemberDeclarationSyntax>());


            foreach (TypeParameter typeParameter in node.TypeParameters)
            {
                csClass = csClass.AddTypeParameterListParameters(typeParameter.ToCsNode <TypeParameterSyntax>());
                if (typeParameter.Constraint != null)
                {
                    csClass = csClass.AddConstraintClauses(SyntaxFactory
                                                           .TypeParameterConstraintClause(typeParameter.Name.Text)
                                                           .AddConstraints(SyntaxFactory.TypeConstraint(typeParameter.Constraint.ToCsNode <TypeSyntax>()))
                                                           );
                }
            }

            List <Node> baseTypes = node.GetBaseTypes(this.Context.Config.PreferTypeScriptType);

            if (baseTypes.Count > 0)
            {
                csClass = csClass.AddBaseListTypes(baseTypes.ToCsNodes <BaseTypeSyntax>());
            }
            if (node.JsDoc.Count > 0)
            {
                csClass = csClass.WithLeadingTrivia(SyntaxFactory.Trivia(node.JsDoc[0].ToCsNode <DocumentationCommentTriviaSyntax>()));
            }

            return(csClass);
        }
コード例 #2
0
        private static ClassDeclarationSyntax GetNewNode(
            ClassDeclarationSyntax classDeclaration,
            TypeParameterConstraintSyntax constraint,
            SemanticModel semanticModel)
        {
            string name = GetTypeParameterName(classDeclaration.OpenBraceToken.SpanStart, semanticModel);

            ClassDeclarationSyntax newNode = classDeclaration.AddTypeParameterListParameters(TypeParameter(Identifier(name).WithRenameAnnotation()));

            if (constraint != null)
            {
                newNode = newNode.AddConstraintClauses(TypeParameterConstraintClause(name, constraint));
            }

            return(newNode);
        }
コード例 #3
0
        private static SyntaxNode GetNewNode(
            ClassDeclarationSyntax classDeclaration,
            TypeParameterConstraintSyntax constraint,
            SemanticModel semanticModel,
            CancellationToken cancellationToken)
        {
            TypeParameterListSyntax typeParameterList = classDeclaration.TypeParameterList;

            INamedTypeSymbol classSymbol = semanticModel.GetDeclaredSymbol(classDeclaration, cancellationToken);

            string name = GetTypeParameterName(classDeclaration.OpenBraceToken.SpanStart, semanticModel);

            ClassDeclarationSyntax newNode = classDeclaration.AddTypeParameterListParameters(TypeParameter(Identifier(name).WithRenameAnnotation()));

            if (constraint != null)
            {
                newNode = newNode.AddConstraintClauses(TypeParameterConstraintClause(name, constraint));
            }

            return(newNode);
        }
コード例 #4
0
ファイル: ClassGenerator.cs プロジェクト: Handy94/asteroid
        private static ClassDeclarationSyntax CreateClassDeclarationSyntax(string className, string[] baseClasses, ClassType classType, List <ConstraintData> constraints)
        {
            ClassDeclarationSyntax syntax = SyntaxFactory.ClassDeclaration(className);

            List <SyntaxToken> tokens = new List <SyntaxToken>();

            tokens.Add(SyntaxFactory.Token(SyntaxKind.PublicKeyword));

            switch (classType)
            {
            case ClassType.Standard: break;

            case ClassType.Partial: tokens.Add(SyntaxFactory.Token(SyntaxKind.PartialKeyword)); break;

            case ClassType.Static: tokens.Add(SyntaxFactory.Token(SyntaxKind.StaticKeyword)); break;

            case ClassType.StaticPartial:
                tokens.Add(SyntaxFactory.Token(SyntaxKind.StaticKeyword));
                tokens.Add(SyntaxFactory.Token(SyntaxKind.PartialKeyword));
                break;
            }

            // Add the public modifier: (public class Order)
            syntax = syntax.AddModifiers(tokens.ToArray());

            // Inherit BaseEntity<T> and implement IHaveIdentity: (public class Order : BaseEntity<T>, IHaveIdentity)
            for (int i = 0; i < baseClasses.Length; i++)
            {
                syntax = syntax.AddBaseListTypes(SyntaxFactory.SimpleBaseType(SyntaxFactory.ParseTypeName(baseClasses[i])));
            }

            for (int i = 0; i < constraints.Count; i++)
            {
                syntax = syntax.AddConstraintClauses(CodeGenerationUtility.CreateConstraintClause(constraints[i]));
            }

            return(syntax);
        }
コード例 #5
0
ファイル: ProxyGenerator.cs プロジェクト: swipswaps/Hagar
        private static ClassDeclarationSyntax AddGenericTypeConstraints(
            ClassDeclarationSyntax classDeclaration,
            INamedTypeSymbol type)
        {
            var typeParameters = GetTypeParametersWithConstraints(type.TypeParameters);

            foreach (var(name, constraints) in typeParameters)
            {
                if (constraints.Count > 0)
                {
                    classDeclaration = classDeclaration.AddConstraintClauses(
                        TypeParameterConstraintClause(name).AddConstraints(constraints.ToArray()));
                }
            }

            if (typeParameters.Count > 0)
            {
                classDeclaration = classDeclaration.WithTypeParameterList(
                    TypeParameterList(SeparatedList(typeParameters.Select(tp => TypeParameter(tp.Item1)))));
            }

            return(classDeclaration);
        }
コード例 #6
0
        private static ClassDeclarationSyntax AddGenericTypeConstraints(ClassDeclarationSyntax classDeclaration, INamedTypeSymbol type)
        {
            classDeclaration = classDeclaration.WithTypeParameterList(TypeParameterList(SeparatedList(type.TypeParameters.Select(tp => TypeParameter(tp.Name)))));
            var constraints = new List <TypeParameterConstraintSyntax>();

            foreach (var tp in type.TypeParameters)
            {
                constraints.Clear();
                if (tp.HasReferenceTypeConstraint)
                {
                    constraints.Add(ClassOrStructConstraint(SyntaxKind.ClassConstraint));
                }

                if (tp.HasValueTypeConstraint)
                {
                    constraints.Add(ClassOrStructConstraint(SyntaxKind.StructConstraint));
                }

                foreach (var c in tp.ConstraintTypes)
                {
                    constraints.Add(TypeConstraint(c.ToTypeSyntax()));
                }

                if (tp.HasConstructorConstraint)
                {
                    constraints.Add(ConstructorConstraint());
                }

                if (constraints.Count > 0)
                {
                    classDeclaration = classDeclaration.AddConstraintClauses(TypeParameterConstraintClause(tp.Name).AddConstraints(constraints.ToArray()));
                }
            }

            return(classDeclaration);
        }