4

Is there a possibility to create partial classes that are grouped in the Solution Explorer like VS does it with code behind classes (eg. Default.aspx and Default.aspx.cs).

I would like to create MyClass.cs and MyClass.partial.cs and they should not be shown as 2 seperate files in the Solution explorer.

1
  • 1
    Why make the class partial then? Simply code it all in one file.
    – Ben S
    Mar 16, 2009 at 19:50

3 Answers 3

10

I've not seen a way to do this within Visual Studio itself, but it is possible to do it by editing the .csproj file. You need to find the place where the file you want to show up under the other file is in the proj file and add a element with the content set to the name of the file you want to be dependent upon.

example:

<Compile Include="MyFile.g.cs">
    <DependentUpon>MyFile.cs</DependentUpon>
</Compile>
5

if you name you file MyClass.designer.cs, it does.

There are a few "magic names" that work (.xaml.cs is another one I think).

As an aside, I think partial classes should not be used for anything else than managing generated code. If you find yourself going down that route because there is too much code to maintain in one file, you should reconsider why there is so much code and break it into smaller classes along the lines you were going to break it down anyway (I'm not saying this is your case, but it's just a piece of advice).

2
  • 1
    You hit the point. I got to work on a huge project with bad design and way to big "I do everything" classes. It cries for refactoring, but it will not be done for several (non technical) reasons. So I tried to make the nightmare a little less scarry.
    – chbu
    Mar 16, 2009 at 20:10
  • I feel for you... Good luck anyway. Mar 16, 2009 at 20:29
0

Not that I know of. Generally, the purpose is to support generated code (ie, you're not maintaining it), so for that purpose, putting it in a separate folder is all you would need.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.