agentleFS
neko3csneko3cs/skills/dotnet-scripting/SKILL.md

dotnet-scripting

Skill0 starsChanged 6 days agoMITView on GitHub

Process data or run ad-hoc .NET scripts by piping C# to `dotnet run -`, with `#:package` for external libraries. Use when running a quick data-processing script or one-liner and .NET is available — prefer this over a Python script in that case.

The file

Files
SKILL.md

データ処理スクリプト作成ガイド(C# file-based apps)

このスキルは、C# file-based apps を使ってファイルを作らずにデータ処理を行う方法を提供します。

適用バージョン: .NET 10 SDK 以降(C# 14 以降)

基本方針

コードは 必ずパイプで dotnet run - に渡して実行 してください。 Set-Content や Out-File 等で .cs ファイルを新規作成してから実行する方法は 禁止 です。

優先順位:

  1. パイプ実行(必須): コード文字列を dotnet run - に直接パイプして実行する
  2. 既存ファイル実行: プロジェクトに既に存在する .cs ファイルを dotnet <file>.cs で実行する

#:package ディレクティブが必要な場合も、パイプ実行で対応できます(後述の「外部ライブラリを使う場合」を参照)。

ワンライナー実行(最優先)

基本

PowerShell:

'Console.WriteLine("Hello!");' | dotnet run -

Bash:

echo 'Console.WriteLine("Hello!");' | dotnet run -

複数行

PowerShell:

@'
var sum = Enumerable.Range(1, 100).Sum();
Console.WriteLine($"合計: {sum}");
'@ | dotnet run -

Bash:

dotnet run - << 'EOF'
var sum = Enumerable.Range(1, 100).Sum();
Console.WriteLine($"合計: {sum}");
EOF

暗黙的に使えるnamespace

以下は using 不要で使えます:

  • System, System.IO, System.Collections.Generic, System.Linq, System.Net.Http, System.Threading, System.Threading.Tasks

標準ライブラリでの処理

JSON処理(パッケージ不要)

json.cs:

using System.Text.Json;

var json = Console.In.ReadToEnd();
var doc = JsonDocument.Parse(json);
Console.WriteLine(doc.RootElement.GetProperty("name"));

実行:

'{"name":"太郎"}' | dotnet json.cs

外部ライブラリを使う場合

#:package ディレクティブが必要な場合も、パイプで実行します。ファイルを作成しないでください。

CSV処理

@'
#:package CsvHelper

using CsvHelper;
using System.Globalization;

using var reader = new StreamReader(args[0]);
using var csv = new CsvReader(reader, CultureInfo.InvariantCulture);

var count = csv.GetRecords<dynamic>().Count();
Console.WriteLine($"レコード数: {count}");
'@ | dotnet run - -- data.csv

Excel処理

@'
#:package ClosedXML

using ClosedXML.Excel;

using var workbook = new XLWorkbook(args[0]);
var worksheet = workbook.Worksheet(1);

foreach (var row in worksheet.RowsUsed())
{
    Console.WriteLine(row.Cell(1).Value);
}
'@ | dotnet run - -- Employee.xlsx

よく使うライブラリ

用途ディレクティブ備考
JSON処理(不要)System.Text.Json が標準で利用可能
CSV処理#:package CsvHelper
Excel処理#:package ClosedXML

実用パターン

標準入力とファイル引数の両対応

var input = args.Length > 0 
    ? File.ReadAllText(args[0])
    : Console.In.ReadToEnd();

Console.WriteLine(input.ToUpper());

使い方:

# ファイルから
dotnet script.cs -- input.txt

# 標準入力から
Get-Content input.txt | dotnet script.cs

エラーハンドリング

try
{
    // 処理
}
catch (Exception ex)
{
    Console.Error.WriteLine($"エラー: {ex.Message}");
    return 1;
}

参考資料

オリジナル

Download now (ZIP)

Does it work?

Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.

No reviews yet

People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.

  • It worked
  • Partly
  • It didn't
What did you use it for? e.g. Reviewing pull requests in a Rails app

Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.

Your trail

Files you open will collect here, so you can find them again.

Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.

Now do this for your own.

Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.