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