13,005
回編集
327行目: | 327行目: | ||
// 非同期処理をシミュレートするために1秒待機 | // 非同期処理をシミュレートするために1秒待機 | ||
await Task.Delay(1000); | await Task.Delay(1000); | ||
} | |||
} | |||
</syntaxhighlight> | |||
<br> | |||
==== YAMLファイルの書き込み ==== | |||
以下の例では、YamlDotNetライブラリを使用して、指定されたYAMLファイルを非同期で書き込んでいる。<br> | |||
<br> | |||
# CreateCompanyDataメソッドは、YAMLに変換するためのデータ構造を生成する。 | |||
#: ネストされたDictionaryとListを使用して、YAMLの階層構造を表現している。 | |||
#: これにより、複雑なYAML構造を柔軟に生成する。 | |||
#: <br> | |||
# WriteYamlFileAsyncメソッドは、データをYAMLに変換して、ファイルに非同期で書き込む。 | |||
#: SerializerBuilderを使用してYAMLシリアライザを設定する。 | |||
#: ここでは、CamelCaseNamingConventionを使用しているが、必要に応じて変更可能である。 | |||
#: SerializeメソッドでデータをYAML文字列に変換する。 | |||
<br> | |||
<syntaxhighlight lang="yaml"> | |||
# 書き込むYAMLファイル | |||
company: | |||
name: Tech Solutions Inc. | |||
founded: 2010 | |||
employees: | |||
- name: Alice Johnson | |||
position: CEO | |||
skills: | |||
- leadership | |||
- strategic planning | |||
- name: Bob Williams | |||
position: CTO | |||
skills: | |||
- programming | |||
- system architecture | |||
offices: | |||
headquarters: | |||
address: 123 Main St, Techville | |||
phone: 555-1234 | |||
branch: | |||
address: 456 Innovation Ave, Codetown | |||
phone: 555-5678 | |||
</syntaxhighlight> | |||
<br> | |||
<syntaxhighlight lang="c#"> | |||
using System; | |||
using System.IO; | |||
using System.Collections.Generic; | |||
using System.Threading.Tasks; | |||
using YamlDotNet.Serialization; | |||
using YamlDotNet.Serialization.NamingConventions; | |||
class Program | |||
{ | |||
static async Task Main(string[] args) | |||
{ | |||
string filePath = "sample.yaml"; // 書き込むYAMLファイルのパス | |||
try | |||
{ | |||
// 会社データを作成 | |||
var companyData = CreateCompanyData(); | |||
// YAMLファイルを非同期で書き込む | |||
await WriteYamlFileAsync(filePath, companyData); | |||
} | |||
catch (Exception ex) | |||
{ | |||
Console.WriteLine($"エラーが発生: {ex.Message}"); | |||
} | |||
} | |||
// companyデータを作成 | |||
static Dictionary<string, object> CreateCompanyData() | |||
{ | |||
return new Dictionary<string, object> | |||
{ | |||
["company"] = new Dictionary<string, object> | |||
{ | |||
["name"] = "Tech Solutions Inc.", | |||
["founded"] = 2010, | |||
["employees"] = new List<Dictionary<string, object>> | |||
{ | |||
new Dictionary<string, object> | |||
{ | |||
["name"] = "Alice Johnson", | |||
["position"] = "CEO", | |||
["skills"] = new List<string> { "leadership", "strategic planning" } | |||
}, | |||
new Dictionary<string, object> | |||
{ | |||
["name"] = "Bob Williams", | |||
["position"] = "CTO", | |||
["skills"] = new List<string> { "programming", "system architecture" } | |||
} | |||
}, | |||
["offices"] = new Dictionary<string, object> | |||
{ | |||
["headquarters"] = new Dictionary<string, string> | |||
{ | |||
["address"] = "123 Main St, Techville", | |||
["phone"] = "555-1234" | |||
}, | |||
["branch"] = new Dictionary<string, string> | |||
{ | |||
["address"] = "456 Innovation Ave, Codetown", | |||
["phone"] = "555-5678" | |||
} | |||
} | |||
} | |||
}; | |||
} | |||
// YAMLファイルを非同期で書き込む | |||
static async Task WriteYamlFileAsync(string filePath, Dictionary<string, object> data) | |||
{ | |||
// YAMLシリアライザの設定 | |||
var serializer = new SerializerBuilder() | |||
.WithNamingConvention(CamelCaseNamingConvention.Instance) | |||
.Build(); | |||
// YAMLにシリアライズ | |||
var yaml = serializer.Serialize(data); | |||
// ファイルに非同期で書き込み | |||
await File.WriteAllTextAsync(filePath, yaml); | |||
} | } | ||
} | } |