Esempio n. 1
0
        public static TypeDeclarationSyntax ExtendsWithStructuralEquality(TypeDeclarationSyntax typeDeclaration, SyntaxGenerator generator, SemanticModel semanticModel, TypeDeclarationSyntax originalClassDeclaration = null)
        {
            originalClassDeclaration = originalClassDeclaration ?? typeDeclaration;
            var className        = typeDeclaration.Identifier.Text;
            var publicProperties = GetPublicProperties(originalClassDeclaration);

            var newClassDeclaration = typeDeclaration.WithMembers(typeDeclaration.Members.AddRange(new[]
            {
                GenerateEqualsMethod(generator, publicProperties, typeDeclaration),
                GenerateEqualsWithObjMethod(generator, typeDeclaration),
                GenerateGetHashCodeMethod(generator, semanticModel, publicProperties)
            }));
            var eqInterface = SyntaxFactory.ParseTypeName($"System.IEquatable<{className}>");

            return(generator.AddBaseType(newClassDeclaration, eqInterface) as TypeDeclarationSyntax);
        }
Esempio n. 2
0
        public virtual SyntaxNode ToSyntaxNode(SyntaxGenerator syntaxGenerator)
        {
            var methodsSyntaxNodes       = Methods.Select(method => method.ToSyntaxNode(syntaxGenerator));
            var internalTypesSyntaxNodes = InternalTypes.Select(internalType => internalType.ToSyntaxNode(syntaxGenerator));

            var declarationModifiers = DeclarationModifiers.None.WithPartial(IsPartial);

            var typeSyntaxNode = syntaxGenerator.ClassDeclaration(Name,
                                                                  accessibility: Accessibility,
                                                                  modifiers: declarationModifiers,
                                                                  members: methodsSyntaxNodes.Union(internalTypesSyntaxNodes));

            foreach (var baseType in BaseTypes)
            {
                var baseTypeSyntaxNode = baseType.ToSyntaxNode(syntaxGenerator);
                typeSyntaxNode = syntaxGenerator.AddBaseType(typeSyntaxNode, baseTypeSyntaxNode);
            }

            syntaxGenerator.AddMembers(typeSyntaxNode, Methods.Select(method => method.ToSyntaxNode(syntaxGenerator)));

            return(typeSyntaxNode);
        }