rksoftware

Visual Studio とか C# とかが好きです

try-convert で .NET Framework プロジェクトを .NET Core プロジェクトにコンバートしてみる

最近 .NET Core が話題です。というかデスクトッパー(※1)的に、WPF や WinForms が .NET Core 3.0 で動くようになって対応が進んできていることに注目しています。

※1:デスクトッパーって何ですか?

■ try-convert

.NET Core のグローバルツールで、旧形式の .csproj を新形式にコンバートしてくれます。
物によっては、ターゲットのフレームワークを .NET Framework から .NET Core にもしてくれます。

インストールはこちら↓

■ 使い方

インストールができたらコマンド プロンプトなどで

try-convert -p <.csproj のファイルパス>

または

try-convert -w <.sln のフォルダパス>

でコンバートします。
プロジェクトをコンバートする場合は -p オプションで .csproj はファイルのパス、ソリューションの場合は、-w オプションで .sln のあるフォルダのパスを指定すると動くようです。
ソリューションの場合は、ソリューションに含まれるプロジェクト全てがコンバート対象になるようです。逆に言うと、その配下のフォルダに有っても .sln に含まれないプロジェクトは対象にならないようです。

■ 試してみた

試してみた結果を設計図共有サイトにアップしてみました。

一例

一番わかりやすい .NET Framework の WPF プロジェクトをコンバートしてみた例です。

<?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>{E5A30344-EDE6-4AB7-9CAC-0D940F825B0F}</ProjectGuid>
    <OutputType>WinExe</OutputType>
    <RootNamespace>WpfApp1net472_jsonnet</RootNamespace>
    <AssemblyName>WpfApp1net472_jsonnet</AssemblyName>
    <TargetFrameworkVersion>v4.7.2</TargetFrameworkVersion>
    <FileAlignment>512</FileAlignment>
    <ProjectTypeGuids>{60dc8134-eba5-43b8-bcc9-bb4bc16c2548};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</ProjectTypeGuids>
    <WarningLevel>4</WarningLevel>
    <AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
    <Deterministic>true</Deterministic>
  </PropertyGroup>
  <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
    <PlatformTarget>AnyCPU</PlatformTarget>
    <DebugSymbols>true</DebugSymbols>
    <DebugType>full</DebugType>
    <Optimize>false</Optimize>
    <OutputPath>bin\Debug\</OutputPath>
    <DefineConstants>DEBUG;TRACE</DefineConstants>
    <ErrorReport>prompt</ErrorReport>
    <WarningLevel>4</WarningLevel>
  </PropertyGroup>
  <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
    <PlatformTarget>AnyCPU</PlatformTarget>
    <DebugType>pdbonly</DebugType>
    <Optimize>true</Optimize>
    <OutputPath>bin\Release\</OutputPath>
    <DefineConstants>TRACE</DefineConstants>
    <ErrorReport>prompt</ErrorReport>
    <WarningLevel>4</WarningLevel>
  </PropertyGroup>
  <ItemGroup>
    <Reference Include="Newtonsoft.Json, Version=12.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
      <HintPath>..\packages\Newtonsoft.Json.12.0.3\lib\net45\Newtonsoft.Json.dll</HintPath>
    </Reference>
    <Reference Include="System" />
    <Reference Include="System.Data" />
    <Reference Include="System.Xml" />
    <Reference Include="Microsoft.CSharp" />
    <Reference Include="System.Core" />
    <Reference Include="System.Xml.Linq" />
    <Reference Include="System.Data.DataSetExtensions" />
    <Reference Include="System.Net.Http" />
    <Reference Include="System.Xaml">
      <RequiredTargetFramework>4.0</RequiredTargetFramework>
    </Reference>
    <Reference Include="WindowsBase" />
    <Reference Include="PresentationCore" />
    <Reference Include="PresentationFramework" />
  </ItemGroup>
  <ItemGroup>
    <ApplicationDefinition Include="App.xaml">
      <Generator>MSBuild:Compile</Generator>
      <SubType>Designer</SubType>
    </ApplicationDefinition>
    <Page Include="MainWindow.xaml">
      <Generator>MSBuild:Compile</Generator>
      <SubType>Designer</SubType>
    </Page>
    <Compile Include="App.xaml.cs">
      <DependentUpon>App.xaml</DependentUpon>
      <SubType>Code</SubType>
    </Compile>
    <Compile Include="MainWindow.xaml.cs">
      <DependentUpon>MainWindow.xaml</DependentUpon>
      <SubType>Code</SubType>
    </Compile>
  </ItemGroup>
  <ItemGroup>
    <Compile Include="Properties\AssemblyInfo.cs">
      <SubType>Code</SubType>
    </Compile>
    <Compile Include="Properties\Resources.Designer.cs">
      <AutoGen>True</AutoGen>
      <DesignTime>True</DesignTime>
      <DependentUpon>Resources.resx</DependentUpon>
    </Compile>
    <Compile Include="Properties\Settings.Designer.cs">
      <AutoGen>True</AutoGen>
      <DependentUpon>Settings.settings</DependentUpon>
      <DesignTimeSharedInput>True</DesignTimeSharedInput>
    </Compile>
    <EmbeddedResource Include="Properties\Resources.resx">
      <Generator>ResXFileCodeGenerator</Generator>
      <LastGenOutput>Resources.Designer.cs</LastGenOutput>
    </EmbeddedResource>
    <None Include="packages.config" />
    <None Include="Properties\Settings.settings">
      <Generator>SettingsSingleFileGenerator</Generator>
      <LastGenOutput>Settings.Designer.cs</LastGenOutput>
    </None>
  </ItemGroup>
  <ItemGroup>
    <None Include="App.config" />
  </ItemGroup>
  <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
</Project>
<Project Sdk="Microsoft.NET.Sdk.WindowsDesktop">
  <PropertyGroup>
    <TargetFramework>netcoreapp3.0</TargetFramework>
    <OutputType>WinExe</OutputType>
    <GenerateAssemblyInfo>false</GenerateAssemblyInfo>
    <UseWPF>true</UseWPF>
  </PropertyGroup>
  <ItemGroup>
    <PackageReference Include="Newtonsoft.Json" Version="12.0.3" />
  </ItemGroup>
  <ItemGroup>
    <PackageReference Include="Microsoft.CSharp" Version="4.7.0" />
  </ItemGroup>
  <ItemGroup>
    <PackageReference Include="System.Data.DataSetExtensions" Version="4.5.0" />
  </ItemGroup>
</Project>

見事に新形式で netcoreapp3.0 になっています。

■ コンバートしてみた

プロジェクトとその結果は次のようになります。

プロジェクト 追加した NuGet パッケージ コンバート結果
netstandard2.0 クラスライブラリ - コンパート対象外(既に新形式)
net472 クラスライブラリ EntityFramework net472 半端なファイル
net472 クラスライブラリ JSON.NET net472 新形式
netcoreapp3.0 WPF - コンパート対象外(既に新形式)
net472 WPF (.sln に含まれない) - 対象外
net472 WPF EntityFramework net472 半端なファイル
net472 WPF JSON.NET netcoreapp3.0 新形式

コンパート対象外(既に新形式) のプロジェクトに対しては

'XXXXX\WpfApp1core30.csproj' is already a .NET SDK-style project, so it won't be converted.

といったメッセージが表示されてコンバートはされません。
net472 半端なファイル となっているプロジェクトは、何かコンバートはされていますが新形式としても動かない? 半端な感じになります。今のところ、EntityFramework を追加するとこれになります。Visual Studio でもコンテキストメニューにビルドやリビルドが出てきません。アンロードしないとソリューションのビルドも失敗します。JSON.NET を追加したプロジェクトは元気にコンバートされビルドや実行ができています。

その他、注目なのは WPF プロジェクトです。このプロジェクトは netcoreapp3.0 にコンバートされています。やった!

■ まとめ

条件はありますが使えます。使う機会がどれほどあるかは人次第で、私はそんなに使う機会はなさそうですが多くのライブラリをメンテナンスしている方は試してみるともしかしたら人生が生きやすくなるかもしれません。