LoginSignup
15

More than 5 years have passed since last update.

Partialクラスのファイルを元クラスの子として表示

Posted at

問題

Visual Studio 2010 (C#) にて。
クラスを分割定義するために partial クラスを複数ファイルに設置したりしますが
ソリューションエクスプローラ上で ~.Designer.cs ファイルは元クラスの子として表示されるのに、
自前で追加した cs ファイルは元クラスの子としては表示されず、フォルダ内に並列に表示される。
これはイケてない。

下の例で言えば、Form1.Hoge.cs は Form1.cs の子要素として表示したい。
vs01.png

まず思いつく方法

直感的にドラッグ&ドロップ・・・は効かないのです。

csprojをいじります

目的のファイルが記述されている行を探し、

sample.csproj (before)
    …
    <Compile Include="Forms\Form1.Hoge.cs" />
    …

こんな感じに書き替えます。

sample.csproj (after)
    …
    <Compile Include="Forms\Form1.Hoge.cs">
      <DependentUpon>Form1.cs</DependentUpon>
      <SubType>Code</SubType>
    </Compile>
    …

DependentUpon で項目の親を指定し、
SubType で項目の種別を指定するわけです。

SubType を指定しないと、純粋なコードファイルであるにも関わらず、Visual Studio はこれをユーザコントロールとして認識してしまうので注意しましょう。(項目をダブルクリック一発でコードが開けない状態になる)

結果

めでたく子扱いになりました。
vs02.png

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
15