예제 #1
0
        public void Run(RefactoringContext context)
        {
            var property = context.GetNode <PropertyDeclaration> ();

            string backingStoreName = context.GetNameProposal(property.Name);

            // create field
            var backingStore = new FieldDeclaration();

            backingStore.ReturnType = property.ReturnType.Clone();

            var initializer = new VariableInitializer(backingStoreName);

            backingStore.Variables.Add(initializer);

            // create new property & implement the get/set bodies
            var newProperty = (PropertyDeclaration)property.Clone();
            var id1         = new IdentifierExpression(backingStoreName);
            var id2         = new IdentifierExpression(backingStoreName);

            newProperty.Getter.Body = new BlockStatement()
            {
                new ReturnStatement(id1)
            };
            newProperty.Setter.Body = new BlockStatement()
            {
                new ExpressionStatement(new AssignmentExpression(id2, AssignmentOperatorType.Assign, new IdentifierExpression("value")))
            };

            using (var script = context.StartScript()) {
                script.Replace(property, newProperty);
                script.InsertBefore(property, backingStore);
                script.Link(initializer, id1, id2);
            }
        }
예제 #2
0
        static PropertyDeclaration GeneratePropertyDeclaration(RefactoringContext context, FieldDeclaration field, VariableInitializer initializer)
        {
            var mod = ICSharpCode.NRefactory.Cpp.Modifiers.Public;

            if (field.HasModifier(ICSharpCode.NRefactory.Cpp.Modifiers.Static))
            {
                mod |= ICSharpCode.NRefactory.Cpp.Modifiers.Static;
            }

            return(new PropertyDeclaration()
            {
                Modifiers = mod,
                Name = context.GetNameProposal(initializer.Name, false),
                ReturnType = field.ReturnType.Clone(),
                Getter = new Accessor()
                {
                    Body = new BlockStatement()
                    {
                        new ReturnStatement(new IdentifierExpression(initializer.Name))
                    }
                }
            });
        }