Пример #1
0
        /// <summary>
        /// 删除属性
        /// </summary>
        /// <param name="classes"></param>
        /// <param name="property"></param>
        public void RemoveProperty(ClassDeclarationSyntax classes, CsharpProperty property)
        {
            var rproperty    = GetClassProperty(classes).FirstOrDefault(x => x.Identifier.Text == property.PropertyName);
            var updatedClass = classes.RemoveNode(rproperty, SyntaxRemoveOptions.KeepNoTrivia);

            roots = roots.ReplaceNode(classes, updatedClass).NormalizeWhitespace();
        }
Пример #2
0
        /// <summary>
        /// 创建新属性
        /// </summary>
        /// <param name="property"></param>
        /// <returns></returns>
        public PropertyDeclarationSyntax CreateProperty(CsharpProperty property)
        {
            var documentcomment     = GeneratorComment(property.PropertyComment);
            var propertyDeclaration = SyntaxFactory.PropertyDeclaration(SyntaxFactory.ParseTypeName(property.PropertyType), property.PropertyName)
                                      // .AddModifiers(SyntaxFactory.Token(SyntaxKind.PublicKeyword))
                                      .AddAccessorListAccessors(
                SyntaxFactory.AccessorDeclaration(SyntaxKind.GetAccessorDeclaration).WithSemicolonToken(SyntaxFactory.Token(SyntaxKind.SemicolonToken)),
                SyntaxFactory.AccessorDeclaration(SyntaxKind.SetAccessorDeclaration).WithSemicolonToken(SyntaxFactory.Token(SyntaxKind.SemicolonToken))).WithModifiers(
                SyntaxFactory.TokenList(
                    new[] {
                SyntaxFactory.Token(
                    SyntaxFactory.TriviaList(
                        SyntaxFactory.Trivia(documentcomment)), // xmldoc
                    SyntaxKind.PublicKeyword,                   // original 1st token
                    SyntaxFactory.TriviaList())
                //, SyntaxFactory.Token(SyntaxKind.PrivateKeyword)
            }));

            return(propertyDeclaration);
        }
Пример #3
0
        /// <summary>
        /// 添加属性
        /// </summary>
        /// <param name="classes"></param>
        /// <param name="property"></param>
        public void AddProperty(ClassDeclarationSyntax classes, CsharpProperty property)
        {
            var updatedClass = classes.AddMembers(CreateProperty(property));

            roots = roots.ReplaceNode(classes, updatedClass).NormalizeWhitespace();
        }