0

I added two partial classes that have the same name:

public partial class SomePartial
{
}

and now I have two separate files.

I want to make them appear as single item in the project, that can be expanded like in a WebForms project where you have the Form.cs and you have the Form.Designer.cs.

Thorarin
  • 47,289
  • 11
  • 75
  • 111
ilay zeidman
  • 2,654
  • 5
  • 23
  • 45
  • 1
    The question title is a bit unfortunate, because you don't really want them in the same file, you just want one file to be shown as a sub-item of the other (if I understand you correctly). The confusion is probably why the question was downvoted. – Medo42 Dec 27 '13 at 09:52

2 Answers2

7

You can do this by editing the .csproj-file by hand.

Right-click the project and select "Unload project", then right-click it again and choose "Edit .csproj".

This is an .xml file which describes your project and its dependencies. You need to find the entries that describe the files of your partial class, e.g.

<ItemGroup>
    <Compile Include="MyPartialClass.cs" />
    <Compile Include="MyPartialClass.Extension.cs" />
</ItemGroup>

Now you can set one as a sub-item of the other by adding a DependendUpon subtag:

<ItemGroup>
    <Compile Include="MyPartialClass.cs" />
    <Compile Include="MyPartialClass.Extension.cs">
        <DependentUpon>MyPartialClass.cs</DependentUpon>
    </Compile>
</ItemGroup>

Save the file, and re-load the project.

Medo42
  • 3,821
  • 1
  • 21
  • 37
  • Visual Studio can also do this automatically based on naming convention. If you create two files, `Foo.cs` and `Foo.designer.cs` it would nest them automatically. If you want something other than `designer` however, this would be the way to go. – Thorarin Dec 27 '13 at 09:51
0

It doesn't have much sense to have a partial class in the same file, IMHO. The main goal of partial classes is to allow more that one person to work with the same class, and if someone checks out this file from your code repo, this facility is gone. Other goal is to keep manual code away from auto-generated code so it don't get lost when regeneration happens. That said, you coul use code region to achive what you want.

#region Partial 1
// Code here
#endregion 

#region Partial 2
// Code here
#endregion
Oscar
  • 13,594
  • 8
  • 47
  • 75