1

I have a file that is generated containing multiple classes that I want to split into multiple files each one containing just one class. The code is in c#

Is there a program that can do this (preferably with source code available)? Is there a simple Regex that can extract the classes/interfaces?

Dan
  • 683
  • 2
  • 8
  • 24
  • Have a look at that question: It uses regular expression to capture well defined C# classes Regular Expressions to find C# class and method names – Rami Oct 14 '13 at 07:00
  • If you are cleaning up a C# project in Visual Studio you can use [ReSharper](http://www.jetbrains.com/resharper/): Right click the project and on the menu select _Refactor_ => _Move Types Into Matching Files...__. – Martin Liversage Oct 14 '13 at 07:15
  • @RamiHelmy where's the link? – Noctis Oct 14 '13 at 07:21
  • Sorry, here it is:http://stackoverflow.com/questions/7593136/regular-expressions-to-find-c-sharp-class-and-method-names – Rami Oct 14 '13 at 07:24

3 Answers3

1

I don't believe Regex would be the correct strategy to parse C# code. It could probably works in some simple cases but you probably face some situation tricking you. Think as an example about having some commentend unbalanced '{' in the code. I suggest to you to investigate this other SO question: Parser for C# about how to parse c# code.

Community
  • 1
  • 1
Felice Pollano
  • 32,832
  • 9
  • 75
  • 115
  • If you properly understand the language (inluding commenting), regex is fine. How do you think the compilers lexer works? – Gusdor Oct 14 '13 at 07:15
  • @Gusdor lexer is just a step in compiling. Think why in sharpdevelop they use a parser instead of plain regex for intellisense. – Felice Pollano Oct 14 '13 at 07:17
  • What I was trying to get at is that it is possible because the language has a defined structure. Regex is just a parsing script. It works char-at-a-time, not line-by-line. – Gusdor Oct 14 '13 at 07:23
0

I think you should try with Visual Studio Macro Programming.

John Saunders
  • 160,644
  • 26
  • 247
  • 397
mit
  • 1,763
  • 4
  • 16
  • 27
0

If that's a one off, do the least thing that (quickly) works. So if it is just one or a few files and you're not after a very generic solution, I would identify all the headers (public class Foo and the like), for example with the help of Notepad++ and a recorded macro, manually correcting the results, and then I'd write a little C# program to split the file in places where these headers are

Konrad Morawski
  • 8,307
  • 7
  • 53
  • 91