Click here to Skip to main content
15,886,362 members
Please Sign up or sign in to vote.
5.00/5 (3 votes)
Hi,

I am trying to find a way to shortcut refactor/encapsulate more than the default in Visual Studio. For example, now if I type this:
C#
private string serverSMTP = string.Empty;

then I can right click, choose Refactor, then Choose Encapsulate and get this:

C#
private string serverSMTP = string.Empty;

public string ServerSMTP
{
    get { return serverSMTP; }
    set { serverSMTP = value; }
}


I'd like to be able to customize it to do this instead, is there any way to accomplish this? And expanding that is there a way to shortcut it? like Ctrl R, E?

C#
private string serverSMTP = string.Empty;
public string ServerSMTP
{
    get { return serverSMTP; }
    set
    {
        if (value != serverSMTP)
        {
            serverSMTP = value;
            RaisePropertyChanged("ServerSMTP");
        }
    }
}
Posted
Updated 18-May-11 10:47am
v2
Comments
Ed Nutting 18-May-11 17:11pm    
Have you tried looking at writing a Visual Studio Macro - that would be the only way I would have thought, to do this.
Sergey Alexandrovich Kryukov 18-May-11 17:21pm    
It's hardly can be Macro, but Add-In would do.
Please see my answer.
--SA
Sergey Alexandrovich Kryukov 18-May-11 17:23pm    
Good question, not very good example -- existing re-factoring type provides a reasonable starting point for making code you want. I'll vote 5 for this question.
Please see my answer.
--SA

1 solution

Good question.

I've read somewhere that customization using Visual Studio code re-factoring engine is planned by Microsoft by not yet delivered. Locate directory where the related snippets are ("[Visual Studio path]\VC#\Snippets\1033\"), just search for "*.snippet" — you will find the sub-directory "Refactoring". You can play with that, but I're read somewhere in Microsoft documentation that it's not recommended. Still you could experiment this this stuff, as you can always backup and restore it. In contrast, I customized my "Visual C#" snippets (not using Visual Studio IDE, as it's too tedious to me, but directly) and was able to add my own snippets and improve existing ones. I'm not sure if it's possible to add re-factoring snippets; probably not because they should be backed by much more complex code.

Another approach is creation of Visual Studio add-in from scratch, using project templates from the node "Other Project Types" => "Extensibility".

You can try available commercial add-ins like Visual Assist X, CodeRush or ReSharper — they are all supposed to support code re-factoring. You can locate these products in Wikipedia.

—SA
 
Share this answer
 
v2
Comments
Ed Nutting 18-May-11 17:24pm    
Good answer, my 5 :) Yes perhaps add-in would be better but a macro would be do-able. It depends what the OP is used to/has worked most with I guess.
Sergey Alexandrovich Kryukov 18-May-11 17:26pm    
I suspect the best would be the improved Microsoft engine they planned for some future version of the Studio. It's very hard to seduce me to use any 3rd-party add-in, commercial or not.
--SA
Ed Nutting 18-May-11 17:28pm    
Yes, but then updates can never come soon enough so if the OP needs it now... I think you get where I'm going ;P (Oh and I've never used a third party add-in, macro or similar - don't see the need. VS2010 seems pretty good to me :) )
Versile 18-May-11 17:41pm    
I accept it must be an add-in, I had one before but suddenly cannot find it again (had to reinstall system)... I was really hoping someone knew of a way to do it or finding the relative add in. Thanks a lot for taking the time to answer.

V
Sergey Alexandrovich Kryukov 18-May-11 17:42pm    
You're welcome.
Thanks for accepting this answer.
Good luck,
--SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900