LoginSignup
4
11

More than 5 years have passed since last update.

Macで.NETのプログラムを書いてみる

Last updated at Posted at 2016-12-19

Windowsでしか使えないと思い込んでいた.NET Frameworkのサブセットが.NET Coreとして公開されていて、MacやLinuxでも使えるようになっていた。

https://dotnet.github.io/

.NET CoreをMacにインストールする

.NET FrameworkをMacで使えるようにするためには、Homebrewのお世話になる。

% brew install Caskroom/cask/dotnet

これでインストールは完了。その後は、export PATH=/usr/local/share/dotnet;$PATHしておけば良いのかな。

.NET CoreでHello World

.NET Coreを使ったC#のプログラムをまずは書いてみるワケだけれど、まずはHello Worldからだ。

% mkdir hello
% cd ./hello
% /usr/local/share/dotnet/dotnet new

すると、project.jsonProgram.csができる。
Program.csの中身は、以下のようになっている。

using System;

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

そして、project.jsonは、次のようになっている。

{
  "version": "1.0.0-*",
  "buildOptions": {
    "debugType": "portable",
    "emitEntryPoint": true
  },
  "dependencies": {},
  "frameworks": {
    "netcoreapp1.1": {
      "dependencies": {
        "Microsoft.NETCore.App": {
          "type": "platform",
          "version": "1.1.0"
        }
      },
      "imports": "dnxcore50"
    }
  }
}

ということで、もうHello Worldのコードができている。

それでは、/usr/local/share/dotnet/dotnet restoreを実行して、実行のための依存環境を整えた後、/usr/local/share/dotnet/dotnet buildする。

% /usr/local/share/dotnet/dotnet build
Project hello (.NETCoreApp,Version=v1.1) will be compiled   because expected outputs are missing
Compiling hello for .NETCoreApp,Version=v1.1

Compilation succeeded.
    0 Warning(s)
    0 Error(s)

Time elapsed 00:00:03.7596930

これを実行するには、/usr/local/share/dotnet/donet runする。

% /usr/local/share/dotnet/dotnet run
Project hello (.NETCoreApp,Version=v1.1) was previously compiled. Skipping compilation.
Hello World!

動いた。

Visual Studio Code上でデバッグ環境を作る

MacではVisualStudioが動かないのはもちろんだが、VisualStudioCodeなら動く。(と思ったら、https://www.visualstudio.com/vs/visual-studio-mac/というページがあった。)

% open -a /Applications/Visual\ Studio\ Code.app .

すると、Required assets to build and debug are missing from 'hello'. Add Them?と聞かれるので、Yesと答える。

デバッグコンソールには、以下のように表示される。

--------------------------------------------------------------------------------
You may only use the C# Extension for Visual Studio Code with Visual Studio
Code, Visual Studio or Xamarin Studio software to help you  develop and test your
applications.
--------------------------------------------------------------------------------

その後は、次の図の通り。特に何かあらためての設定等をしなくても、一通りは動かすことができる。

20161219.png

C#のExtensionがVisual Studio Code.appに入っていれば、とりあえずコード入力時のアシストもある程度効くし、ファイルの読み書きをする程度の簡単なアプリケーションなら、C#で書くことができそう。

まあ、その程度であれば、ライブラリが充実しているPerl、Ruby、PythonといったLightweight Languageでやるのが簡単ですけどね。

.NET Core その他

donet コマンドで何ができるのか、--help オプションを付けて実行してみる。

Common Commands:
  new           Initialize a basic .NET project
  restore       Restore dependencies specified in the .NET project
  build         Builds a .NET project
  publish       Publishes a .NET project for deployment (including the runtime)
  run           Compiles and immediately executes a .NET project
  test          Runs unit tests using the test runner specified in the project
  pack          Creates a NuGet package

これらのCommandsに対応するVisual Studio Code上での操作の仕方はほとんど分からない(例えば、どうやってunit testをVisual Studio Codeのデバッグ環境で実行するんだろう?)んだけれど、それはおいおい調べていくことにする。

4
11
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
4
11