当前位置: 首页>>代码示例>>C#>>正文


C# PropertyDeclarationSyntax.HasSetter方法代码示例

本文整理汇总了C#中Microsoft.CodeAnalysis.CSharp.Syntax.PropertyDeclarationSyntax.HasSetter方法的典型用法代码示例。如果您正苦于以下问题:C# PropertyDeclarationSyntax.HasSetter方法的具体用法?C# PropertyDeclarationSyntax.HasSetter怎么用?C# PropertyDeclarationSyntax.HasSetter使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在Microsoft.CodeAnalysis.CSharp.Syntax.PropertyDeclarationSyntax的用法示例。


在下文中一共展示了PropertyDeclarationSyntax.HasSetter方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: VisitPropertyDeclaration

        public override SyntaxNode VisitPropertyDeclaration(PropertyDeclarationSyntax propertyDeclaration)
        {
            // Check each node until we find the property in question. When we have it, we'll replace it with an Auto Property
            if (propertyDeclaration == this._fullProperty)
            {
                // Create Empty getters and setters.
                var emptyGetter = SyntaxFactory.AccessorDeclaration(SyntaxKind.GetAccessorDeclaration).WithSemicolonToken(SyntaxFactory.Token(SyntaxKind.SemicolonToken));
                var emptySetter = SyntaxFactory.AccessorDeclaration(SyntaxKind.SetAccessorDeclaration).WithSemicolonToken(SyntaxFactory.Token(SyntaxKind.SemicolonToken));

                if (propertyDeclaration.HasGetter())
                {
                    // Put the original Get modifier on the Auto property
                    emptyGetter = emptyGetter.WithModifiers(propertyDeclaration.GetGetter().Modifiers);
                }
                else
                {
                    // Full property didn't have a getter, but no get in a Auto property makes no sense... We'll keep a get, but make it private
                    emptyGetter = emptyGetter.WithModifiers(SyntaxFactory.TokenList(SyntaxFactory.Token(SyntaxKind.PrivateKeyword)));
                }

                if (propertyDeclaration.HasSetter())
                {
                    // Put the original Set modifier on the Auto property
                    emptySetter = emptySetter.WithModifiers(propertyDeclaration.GetSetter().Modifiers);
                }
                else
                {
                    // Full property didn't have a setter, but no set in an Auto property makes no sense... We'll keep a set, but make it private
                    emptySetter = emptySetter.WithModifiers(SyntaxFactory.TokenList(SyntaxFactory.Token(SyntaxKind.PrivateKeyword)));
                }

                // Create a new auto property (without a body)
                var newProperty = _fullProperty.WithAccessorList(
                        SyntaxFactory.AccessorList(
                            SyntaxFactory.List(new[] { emptyGetter, emptySetter })));

                return newProperty;
            }

            return base.VisitPropertyDeclaration(propertyDeclaration);
        }
开发者ID:NielsFilter,项目名称:PropertyExplosion,代码行数:41,代码来源:PropertyCruncher.cs

示例2: VisitPropertyDeclaration

        public override SyntaxNode VisitPropertyDeclaration(PropertyDeclarationSyntax property)
        {
            // Check each node until we find the property in question. When we have it, we'll replace it with a Full Property
            if (property.IsEquivalentTo(this._crunchedProperty))
            {
                AccessorDeclarationSyntax getter = null;
                AccessorDeclarationSyntax setter = null;

                SyntaxTokenList getterModifiers = default(SyntaxTokenList);
                SyntaxTokenList setterModifiers = default(SyntaxTokenList);

                bool hasGetter = false;
                bool hasSetter = false;

                if (property.IsExpressionProperty())
                {
                    hasGetter = true;
                }
                else
                {
                    hasGetter = property.HasGetter();
                    hasSetter = property.HasSetter();

                    if (hasGetter)
                    {
                        getterModifiers = property.GetGetter().Modifiers;
                    }
                    if (hasSetter)
                    {
                        setterModifiers = property.GetSetter().Modifiers;
                    }
                }

                if (hasGetter) // Check if original Auto Property had a getter
                {
                    // Create a new Getter with a body, returning a private field
                    getter = SyntaxFactory.AccessorDeclaration(SyntaxKind.GetAccessorDeclaration).WithBody(
                                    SyntaxFactory.Block(
                                        SyntaxFactory.ReturnStatement(SyntaxFactory.IdentifierName(this.PrivateFieldName))) // return field e.g. return _myProperty;
                                 )
                                 .WithModifiers(getterModifiers) // Keep original modifiers
                                 .WithoutTrivia();
                }
                if (hasSetter) // Check if original Auto Property had a setter
                {
                    // Create a new Setter with a body, setter the private field
                    setter = SyntaxFactory.AccessorDeclaration(SyntaxKind.SetAccessorDeclaration).WithBody(
                                SyntaxFactory.Block(
                                    SyntaxFactory.ExpressionStatement(
                                        SyntaxFactory.AssignmentExpression(SyntaxKind.SimpleAssignmentExpression, SyntaxFactory.IdentifierName(this.PrivateFieldName), SyntaxFactory.IdentifierName("value")))) // Assignment e.g. _myProperty = value
                             )
                             .WithModifiers(setterModifiers) // Keep original modifiers
                             .WithoutTrivia();
                }

                // Create a new property. Set the type and name
                var newProperty = SyntaxFactory.PropertyDeclaration(property.Type, property.Identifier.ValueText)
                    .WithModifiers(property.Modifiers); // use the modifier(s) of the original property

                // Add getter and setter to accessor list
                var accessors = new List<AccessorDeclarationSyntax>();
                if (getter != null)
                {
                    accessors.Add(getter);
                }
                if (setter != null)
                {
                    accessors.Add(setter);
                }

                // Put together the property with our built up accessors list
                newProperty = newProperty.WithAccessorList(
                        SyntaxFactory.AccessorList(
                            SyntaxFactory.List(accessors)));

                return newProperty; // Returning our new property "replaces" the original
            }

            return base.VisitPropertyDeclaration(property);
        }
开发者ID:NielsFilter,项目名称:PropertyExplosion,代码行数:80,代码来源:PropertyExploder.cs


注:本文中的Microsoft.CodeAnalysis.CSharp.Syntax.PropertyDeclarationSyntax.HasSetter方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。