旧式C#プロジェクト(csproj)をSDKスタイルへ移行する方法
旧式プロジェクトとは下記のような形式で、従来の.NET では一般的なものでした。
<?xml version="1.0" encoding="utf-8"?> <Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> <Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" /> <PropertyGroup> <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration> <Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform> <ProjectGuid>{063ACA49-2EDC-4FF3-9D4B-6CE9ED74CE37}</ProjectGuid> <OutputType>Library</OutputType> <AppDesignerFolder>Properties</AppDesignerFolder> <RootNamespace>TsGui</RootNamespace> <AssemblyName>TsGui</AssemblyName> <TargetFrameworkVersion>v4.6.1</TargetFrameworkVersion> <FileAlignment>512</FileAlignment> <Deterministic>true</Deterministic> </PropertyGroup> <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' "> <DebugSymbols>true</DebugSymbols> <DebugType>full</DebugType> ...
一方SDKスタイルとは<Project Sdkから始まる新しい形式のプロジェクトファイルです。 詳細な違いについてはここでは解説しません。
<Project Sdk="Microsoft.NET.Sdk.WindowsDesktop"> <PropertyGroup> <TargetFramework>netcoreapp3.0</TargetFramework> <OutputType>Library</OutputType> <GenerateAssemblyInfo>false</GenerateAssemblyInfo> <UseWPF>true</UseWPF> </PropertyGroup> <ItemGroup> <Reference Include="System.Windows.Interactivity, Version=4.5.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL" /> </ItemGroup> <ItemGroup> <PackageReference Include="Microsoft.CSharp" Version="4.7.0" /> <PackageReference Include="Microsoft.Xaml.Behaviors.Wpf" Version="1.1.3" /> <PackageReference Include="System.Data.DataSetExtensions" Version="4.5.0" /> </ItemGroup> </Project>
移行するモチベーションとしては.NET Coreへの移行が主な目的ですが他にもプロジェクトに内包されるcsファイルリストが従来浜ではホワイトリスト方式( 必要な .csを記述していく )だったものがブラックリスト形式( 不必要なものを記述する )形になったのでチーム開発をしている場合csprojのコンフリクトが減るといったメリットもあります。
具体的な移行手段としては.NETが提供しているツールを利用します。 https://github.com/dotnet/try-convert
power shellを起動し以下のコマンドを実行
dotnet tool install -g try-convert
Install完了後に下記コマンドでSDKスタイルへのアップグレードを試みます。 いくつかのプロジェクトで試していますが、とりあえず失敗した例は無いです。
try-convert -p hogegoe.csproj
但し、場合によっては参照関係が崩れコンパイルエラーが発生することがあるのでこれを修正します。 自分の手元では System.Interactiveの参照切れに伴う問題が出ていたので この機会にMicrosoft.Xaml.Behaviorsに置き換えました。
また、複数のプラットフォームを指定する場合は<TargetFramework>netcoreapp3.0</TargetFramework>
を
下記のように置き換えます。 TargetFrameWorksと複数形になっているのに注意してください。
この場合bin以下にそれぞれのプラットフォーム向けのアセンブリが出力されます。
<TargetFrameworks>netcoreapp3.1;net461</TargetFrameworks>