9

I'm using Microsoft Visual C# 2010.

I have one class in two files:

file1.cs

partial class SomeClass {
    // something
}

file2.cs

partial class SomeClass {
    // something else
}

Now, in some third place in project, when I write "SomeClass.SomeMethod()" and press Alt+Shift+F10, and pick "generate method stub..." it always creates code in file1.cs.

My question is: Is it possible to give directive to do it in file2.cs, and how?

  • 5
    reSharper offers this option in case you are considering using it. – phil soady Dec 16 '13 at 12:39
  • 2
    why do yo need such thing? – Ktt Dec 16 '13 at 12:43
  • 1
    No, it's not possible with the built-in VS feature – Thomas Levesque Dec 16 '13 at 12:52
  • @Ktt In short: I made a tool that generates a basic code for a set of classes, and that is in one file. Some of these classes needs additional methods and/or attributes. By that division, if i want to change the content of basic code of all classes I just need to change algorithm, regenerate code and overwrite corresponding file. If all is mixed, it would be much harder. It's not much work to cut/paste code, but if I can do it more efectively, why not :) – MirrorImage82 Dec 16 '13 at 13:08
  • I mean why it bothers to have the method in file1.cs, all in all they both points one class since they are partial. Are you trying to order your methods alphabetically methods starts with "a" goes to file1.cs the others in file2.cs :) – Ktt Dec 16 '13 at 13:19
  • @Ktt: You don't seem to understand the fundamental purpose of partial classes in the first place. http://stackoverflow.com/questions/3601901/why-use-partial-classes – BoltClock Dec 16 '13 at 15:42
  • @BoltClock'saUnicorn I do know the purpose, when you have thousands of lines, you make it partial in order to manage it easily, what I meant was different. If he is trying to do what meant then it makes sense – Ktt Dec 16 '13 at 16:06
  • 3
    After I gave up from searching for solution, light blub turned on and one stupid idea came in. I just renamed file, so the one where I want generated code to be stored be the first in alphabetical order. It worked! Maybe this is not general solution, but I got what I wanted. Now it's time for real problems :) Thank you – MirrorImage82 Dec 16 '13 at 16:59

3 Answers3

1

You can use the Resharper function for it. (you can install it via the extension manager)

real_yggdrasil
  • 1,213
  • 4
  • 14
  • 27
0

No, it's not possible by default installation of VS2010

Barsham
  • 749
  • 8
  • 30
0

You should be able to create the code generation parts with the following file naming convention

  • SomeClass.cs
  • SomeClass.Generated.cs
  • SomeClass.DataAccess.cs

All those files will group together with the same code in each file:

public partial class SomeClass{}

When you look at the csproj raw file you will see something like

<Compile Include="SomeClass.cs" />
<Compile Include="SomeClass.DataAccess.cs">
  <DependentUpon>SomeClass.cs</DependentUpon>
</Compile>
<Compile Include="SomeClass.Generated.cs">
  <DependentUpon>SomeClass.cs</DependentUpon>
</Compile>

When you look at the layout within the VS Solution Explorer all the "SomeClass.ChildMethodParts.cs" files will appear under the parent "SomeClass.cs" in the tree. Keeps the whole solution very clean.

Alternatively there is an Addin to VS that includes this functionality as a menu item. See http://vscommands.squaredinfinity.com/Features-SolutionExplorer#solutionexplorer-group

Jamie Clayton
  • 1,007
  • 11
  • 24