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)); }
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)); }