StyleCop mandates a particular sort order for the contents of C# files. For example, fields should be declared before methods and public declarations should come before private ones.

It would be useful to have a Visual Studio extension to move these things into that order automatically.

Does a good extension exist for this (I've found CodeSorter, but the feedback suggests it's very buggy)? Or are there any extensions or shortcuts that make it less painful to reorder this content manually?

有帮助吗?

解决方案

You can try with Regionerate :

Regionerate is an automated tool which helps you to preserve your code's layout over time. Regionerate lets you define regions in your code and determine the way members (fields, methods, properties etc.) should be placed inside them. Regionerate is a zero-friction tool - when setting up, you can choose a Code Layout (the way you want your code to look) or just use the default Code Layout. From that moment on, Regionerate will make sure your code follows that Code Layout.

其他提示

ReSharper can do this. It has a "Type Members Layout" feature that allows you to use either a default set of rules, or to define your own.

CodeMaid is a free and open source Visual Studio extension that will reorganize code to follow StyleCop conventions. You can find it here: http://visualstudiogallery.msdn.microsoft.com/76293c4d-8c16-4f4a-aee6-21f83a571496

Disclaimer: This is a totally shameless plug, I wrote it. ;)

I use ReSharper and their Type Member Layout. See my post for more details.

If Code Sorting is your only concern, you can try the Visual Studio Extension CodeSorter

CodeSorter is highly customizable extension that allows its users to sort C# code itby various conditions such as names, types (method, class, struct, ...), visibility modifiers (private, public, ...), constness modifiers (const, static, readonly) and virtuality modifiers (virtual, override, new, ...).

It is possible to assign multiple search criteria to have the same priority, for example place protected and private members first, and then publics and internals.

Names are compared in a way that leading "m_" and "_" is stripped. This allows fields and properties (eg. _foo and Foo) to be right next to each other.

One-lined declarations are placed without delimiting blank line, while multi-lined declarations are separated by a blank line. The exception is that one-lined field followed by a multi-lined property without comment of the same name will be placed without the delimiting blank line.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top