LoginSignup
1
1

More than 3 years have passed since last update.

Visual Studio ソリューションテンプレート

Last updated at Posted at 2020-03-28

1.環境

Visual Studio 2019 Community 2019 Version 16.4.3
Windows10 64bit 1809

2.テンプレートの作成

2-1.プロジェクトの作成

[C#]-[Windows]-[機能拡張]を選択し、"C# Project Template"を選択。

image.png

#2-2.参考情報

3. ソリューションテンプレートの作り方

3-1. ソリューションテンプレートファイルの修正

.vstemplateファイルのTypeを"Project"から"ProjectGroup"にする。

ProjectCollectionタグにProjectTemplateLinkを追加する。


    <ProjectCollection>

      <ProjectTemplateLink ProjectName="$projectname$.Domain" CopyParameters="true">

        DDDSample.Domain\MyTemplate.vstemplate

      </ProjectTemplateLink>

      <ProjectTemplateLink ProjectName="$projectname$.Infrastructure" CopyParameters="true">

        DDDSample.Infrastructure\MyTemplate.vstemplate

      </ProjectTemplateLink>


    </ProjectCollection>

3-2.各プロジェクトファイルのテンプレート作成

ソリューションに含まれているプロジェクトの数だけ、テンプレートのエクスポートを実行する。

・テンプレートの種類の選択では「プロジェクトのテンプレート」を選択
・テンプレートのオプションの選択では「テンプレートを自動的にVisual Studioにインポート」のチェックを外す

作成されたzipは展開する。

3-3.各プロジェクトの依存関係の設定

プロジェクト内の*.csprojファイルを編集する必要がある。

例として「MyApp」というソリューションに「MyApp.Domain」と「MyApp.Infrastructure」というプロジェクトが存在するとする。
InfrastructureはDomainを参照する場合について説明する。なお
「MyApp」はテンプレートなので可変で、Domain、Infrastructureは固定となる。

Infrastructureのcsprojファイルを開き、以下のように設定する。


  <ItemGroup>
    <ProjectReference Include="..\$ext_safeprojectname$.Domain\$ext_safeprojectname$.Domain.csproj">
      <Project>{97b2ddcf-cb5a-4e25-aa31-671f23f18f1f}</Project>
      <Name>$ext_safeprojectname$.Domain</Name>
    </ProjectReference>
  </ItemGroup>

3-4.Testプロジェクトの参照エラー

Nugetパッケージの復元しても参照エラーが解決されなかった。
Nugetパッケージの参照パスの階層を修正する必要があった。

修正前:<HintPath>..\packages\~
修正後:<HintPath>..\..\packages\~

ソリューションを右クリックして、Nugetパッケージの復元を行った後に
パッケージマネージャコンソールで「Update-Package -Reinstall」
を行うと、参照エラーは解決する。

1
1
0

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
1
1