[MonoDevelop] RefactoringCommands: Encapsulate Fields initial implementation

Lluis Sanchez lluis at ximian.com
Tue Apr 24 11:29:56 EDT 2007

El dt 24 de 04 del 2007 a les 13:40 +0200, en/na Miguel Ángel Pérez va
> Hello all!
> I have been doing some coding with the encapsulate field command and i
> have got an initial implementation that generates the code for the
> property and changes the visibility for the field if its public or
> protected to private. 

Thanks for the patch!

> To do the code generation I've created an internal helper class named
> CodeGeneratorHelper in the class CodeRefactorer
> (Monodevelop.Ide.Projects.CodeGeneration namespace) that implements
> it. 
> I don't know if there is already some class that can generate CodeDom
> namespace objects from an IMember so i have implemented something. 
> What i haven't done is renaming the references to the property, since
> is a thing that Lluis must know better than nobody, can you suggest
> how it could be done?

You can use CodeRefactorer.FindMemberReferences to get a list of
references. However, this is not about renaming references, but about
changing a field reference by a property reference, and this is a
language-specific operation.

> I've also done some changes to BaseRefactorer class to allow a
> parameter with the buffer to modify so the change is done to the
> editor buffer instead of the file in disk. This makes necessary to
> call the methods with an unboxing instead of using the interface
> IRefactorer. 
> It's look like this class could be improved with changes like this
> that helps in the operations needed to implement more refactoring
> commands.

CodeRefactorer has a TextFileProvider property which allows setting an
ITextFileProvider implementation. This interface implementation will
then be used by CodeRefactorer and RefactorerContext to get the files to
read or modify.

Notice that the follow method signature in BaseRefactorer is incorrect:

public CodeTypeMember RenameMember (RefactorerContext ctx, IClass cls, IMember member, string newName, IEditableTextFile file)

because the location of an IClass is language dependent. For example, if
cls is a partial class, RenameMember should rename the class in all
files that implement that class, so the 'file' parameter here doesn't

If you want CodeRefactorer to modify files from the editor, you should
create a ITextFileProvider implementation which returns such files. You
can look in MonoDevelop.GtkCore for an example. Look for the class
OpenDocumentFileProvider. Maybe we should move this class to
MonoDevelop.Ide.Gui, so it can be reused.

I also agree with the comments by Ben. Also, it looks like the indenting
of the code is not consistent, maybe you are mixing tabs and spaces for


More information about the Monodevelop-list mailing list