可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
I always find myself needing to enclose a block of code in curly braces { }, but unfortunately that isn't included in the C# surround code snippets, which seems to be an oversight. I couldn't find anything on building your own surround snippets either (just other kinds of snippets).
I am actually running Resharper too, but it doesn't seem to have this functionality either (or I haven't figured how to activate it).
We have a coding standard of including even a single line of code after an if or else in curly braces, so if I could just make Resharper do that refactoring automatically that would be even better!
回答1:
Here is a quick and dirty snippet to do just that.
To Install:
Save the code as SurroundWithBraces.snippet
into "<my documents>\Visual Studio Version\Code Snippets\Visual C#\My Code Snippets"
To use:
Select block of text.
Press Ctrl+K, Ctrl+S
Chose My Code Snippets, braces
<?xml version="1.0" encoding="utf-8" ?>
<CodeSnippets xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
<CodeSnippet Format="1.0.0">
<Header>
<Title>braces</Title>
<Shortcut>braces</Shortcut>
<Description>Code snippet to surround a block of code with braces</Description>
<Author>Igor Zevaka</Author>
<SnippetTypes>
<SnippetType>Expansion</SnippetType>
<SnippetType>SurroundsWith</SnippetType>
</SnippetTypes>
</Header>
<Snippet>
<Code Language="csharp">
<![CDATA[{
$selected$ $end$
}]]>
</Code>
</Snippet>
</CodeSnippet>
</CodeSnippets>
回答2:
In ReSharper 4.5, curly braces are included as one of the built-in 'Surround Templates':
- Select the text that you want curly
braces around.
ReSharper -> Code -> Surround
With... -> {}
or
ALT + R -> C -> S -> 7
or
Ctrl+E, U -> 7
(Visual Studio scheme)
or
Ctrl+Alt+J -> 7
(ReSharper 2.x/IDEA scheme)
回答3:
How about:
Ctrl-X, {, Ctrl-V, }
You could even bind that to a macro.
回答4:
In VS2015 there is an experimental feature that supports it by selecting the text and typing in }.
See here how to enable.
回答5:
Make your own custom code snippet for doing that.
You can use snippy to create your own http://blogs.msdn.com/gusperez/articles/93681.aspx
or just use an XML editor to create one.
Put the file in My Documents\Visual Studio XXXX\Code Snippets\C#\My Code Snippets
回答6:
To complete Ray Vega's answer, for those using Resharper, I figured out you can associate a shortcut to Resharper commands.
Just do the following (I am using VS 2010):
go to Tools->Options
In the listbox, extend Environment and click on Keyboard.
In the field under "Show commands containing:" enter "resharper.resharper_surroundwith"
In the field under "Press shortcut keys" enter your shortcut (eg: I choose Ctrl+R,Ctrl+S) and click Assign and then Ok.
That's it. you can select your code, and type that shorcut to view all Resharper SurroundWith commands. Just enter 7 to put braces.
回答7:
Edit: This turns out to be part of DxCore, from DevExpress. Leaving here so others notice, but basically I was wrong wrong wrong. To make this particular menu go away you disable it in the 'add ins' dialog; unloading devexpress from their own menu just unloads CodeRush/Refactor, not the base support libraries.
The is (not!) a built in way to do it. I don't know if you can bind a key to it or not. Also, this embed doesn't do anything if you only select one line, so it probably won't work right if your stuff is on one line after the "if".
- Select the block
- Right Click
- Choose "Embed Selection"
- Choose "Block {}"
Note: I have DexExpress installed, but this menu is there even when it isn't loaded, and I could swear it is there even when it isn't installed. However, if I am mistaken...
This honestly seems like something that would be best to ask r# for, a user contrib perhaps?
回答8:
You can wrap a code block with braces by
- Highlight the code block
- Ctrl e -> Ctrl u
- select option 7
I know this is an old question but I hope it helps someone
Ref: Wrapping multiple statements in braces