Hello DotNet5!

Visual Studio Code縛り

.net5のSDKのインストール、及び単純なコード(エディタで直書き)のビルドに問題は何もなかった。が、VScodeでOmnisharpが古いSDKを見ているようで、ビルドどころか補完すら効かない状態だった。
net5_vscode_failed_load_project.png
VScodeのC#Extensionのreadmeを見たところ、.net5使うにはMSBuildの16.8.0が必要とのこと。ん?MSBuild?仕方がないのでVisual Studio 2019のUpdateを行ったところ、VScodeも.net5のSDKを見に行くようになった。.net5のSDKにincluded MSBuild 16.8.0って書いてあるんだけどなー。"net5.0-windows"などでも検索してみたが、同様な事象で躓いている人間を確認できなかった。やはりCSharperはVisual Studio使いがほとんどなんじゃないじゃろか。

実行時バージョン取得

せっかくなので実行時exeのバージョンを取得したい。PHPだと

phpversion(); //-> 7.2.34
Phalcon\Version::get(); //-> 3.4.5

このように実行時のバージョンが容易く取得できるのだが、.netは昔から何故かめんどくさい。今回は https://github.com/nishy2000/DotNetDetector を参考にして簡易的にバージョンを取得した。.netcore3.1 / 5.0と違う事だけを確認したかったので下記のように書いたが、もっと細かく取得したい人はこのパッケージをNugetでゲットするかリポジトリのSourceを確認してください。

using System;
using System.Reflection;
using System.Runtime.Versioning;

namespace HelloDotNet5
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Hello World!");
            Console.WriteLine(DotNetVersionOnThisProgram.Get());
        }
    }

    // <summary>
    /// via) NishySoftware.DotNetDetector https://github.com/nishy2000/DotNetDetector
    /// </summary>
    public static class DotNetVersionOnThisProgram
    {
        public static string Get()
        {
            return ((TargetFrameworkAttribute)Assembly.GetEntryAssembly().GetCustomAttribute(typeof(TargetFrameworkAttribute))).FrameworkName;
        }
    }
}
Hello World!
.NETCoreApp,Version=v5.0

追記

そんなわけで現在作成中の画面キャプチャツールも.net5に移行した。プロジェクトファイルを

<Project Sdk="Microsoft.NET.Sdk.WindowsDesktop">
  <PropertyGroup>
    <OutputType>WinExe</OutputType>
    <TargetFramework>netcoreapp3.1</TargetFramework>
    <UseWindowsForms>true</UseWindowsForms>
    <ApplicationIcon>resources\camera_icon.ico</ApplicationIcon>
  </PropertyGroup>
  <ItemGroup>
    <Content Include="log\.gitkeep">
      <CopyToOutputDirectory>Always</CopyToOutputDirectory>
    </Content>
    <EmbeddedResource Include="resources\camera_icon.ico" LogicalName="camera_icon.ico" />
  </ItemGroup>
</Project>

これを

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <OutputType>WinExe</OutputType>
    <TargetFramework>net5.0-windows</TargetFramework>
    <UseWindowsForms>true</UseWindowsForms>
    <ApplicationIcon>resources\camera_icon.ico</ApplicationIcon>
  </PropertyGroup>
  <ItemGroup>
    <Content Include="log\.gitkeep">
      <CopyToOutputDirectory>Always</CopyToOutputDirectory>
    </Content>
    <EmbeddedResource Include="resources\camera_icon.ico" LogicalName="camera_icon.ico" />
  </ItemGroup>
</Project>

こう変えてから、launch.jsonのnetcoreapp3.1を

{
   // Use IntelliSense to find out which attributes exist for C# debugging
   // Use hover for the description of the existing attributes
   // For further information visit https://github.com/OmniSharp/omnisharp-vscode/blob/master/debugger-launchjson.md
   "version": "0.2.0",
   "configurations": [
        {
            "name": ".NET Core Launch (console)",
            "type": "coreclr",
            "request": "launch",
            "preLaunchTask": "build",
            // If you have changed target frameworks, make sure to update the program path.
            "program": "${workspaceFolder}/bin/Debug/net5.0-windows/Butler.exe",
            "args": [],
            "cwd": "${workspaceFolder}/bin/Debug/net5.0-windows/",
            // For more information about the 'console' field, see https://aka.ms/VSCode-CS-LaunchJson-Console
            "console": "integratedTerminal",
            "stopAtEntry": false,
            "requireExactSource": false
        },
        {
            "name": ".NET Core Attach",
            "type": "coreclr",
            "request": "attach",
            "processId": "${command:pickProcess}"
        }
    ]
}

net5.0-windowsに変更。そしてdotnet cleanをしてからbuildしなおした。

dotnet5_messagebox_show_test

適当に実行して5.0を確認。