6

Is there a tool to merge partial classes into one non-partial class?

I cannot use Reflector, since I need variable names, etc. to match.

EDIT:

I have hundreds of partial classes I want to merge, so cut-paste is not realistic.

cm007
  • 1,352
  • 4
  • 20
  • 40
  • 1
    You mean merge the files into one file per class? – C.Evenhuis May 04 '12 at 13:30
  • 1
    Write your own Tool. It shouldn't be that difficult – BlackBear May 04 '12 at 13:33
  • Write your own Tool. It shouldn't be that difficult and share it with us :) – nawfal May 04 '12 at 13:52
  • 3
    For those suggesting that this is not a difficult to do by oneself: There are many task involved, like merging and cleaning namespaces, sorting them, taking care of class attributes, and sorting all the members inside the class. You don't want to see properties, then methods, then more properties, etc. So this is not a trivial task. –  May 04 '12 at 14:18
  • 1
    For all those saying Copy and Paste, if that were the answer for all refactorings, you wouldn't have ReSharper... it's simple to manually extract something into a variable, but CTRL+R+V (or whatever your shortcut for ReSharper, or any other refactoring tool) is much simpler, more efficient and less error prone. I find those answers lazy and arrogant. A tool like this would be great (and if it doesn't exist, obviously an answer is write your own... I don't understand that either). – jamiebarrow Aug 15 '12 at 12:03
  • One possible reason for the merging: scaffolding. The scaffolded stuff in one partial, and custom stuff in another partial. Scaffold everything, do custom stuff, scaffold again, when you're happy that you're finished you could merge them into one class before committing it to source control [I think Spring Roo or the Play Framework, or some Java stuff has something like that] – jamiebarrow Aug 15 '12 at 12:15

4 Answers4

4

Resharper has a "Copy Type" feature that allows to copy all parts of a type into a single file: http://www.jetbrains.com/resharper/documentation/reviewers_guide.html#refactorings

There is a manual effort involved, though, since you have to open at least one file for each file and invoke the functionality.

4

I know someone else has mentioned using ReSharper, I'm using ReSharper 6.1, and if I click on the partial keyword in a partial class file, and hit ALT+ENTER it gives an option 'Merge partial declarations'.

I think if it's quite a complicated class it struggles to do this though. But I tested it on a simpler version with two different files and one partial class defined in both with different properties, and it managed to merge them fine.

Edit: A custom tool that does this, I'm not sure of. Sorry if this doesn't answer your original question (only noticed you can't use Reflector now, so I assume ReSharper is also out of the question).

jamiebarrow
  • 2,473
  • 3
  • 30
  • 51
1
Edit -> Copy -> Paste. 

Don't know any other simplier tool for this.

Tigran
  • 61,654
  • 8
  • 86
  • 123
  • @HenkHolterman: I repeat "Don't know any other simplier tool for this". – Tigran May 04 '12 at 13:34
  • @Tigran - Mainly because most people wouldn't even consider even doing this, partial classes were added to .NET for a reason, they didn't always exist. – Security Hound May 04 '12 at 14:25
0

Just copy the code from one file to the other, remove the first file and the partial keyword from the other.

If you have hundreds of partial classes, it should be easy enough to make a bot to do just that, only x100 times.

SimpleVar
  • 14,044
  • 4
  • 38
  • 60