[英]How do I specify the date locale when Dserializing YAML using YamlDotNet?
在 YAML 文檔中,我有一個使用 EN-GB 語言環境格式化的日期(所以 07/02/2019 是 2019 年 2 月 2 日)
當我使用 YamlDotNet 反序列化文檔時,它將此解釋為 EN-US 日期,因此將其存儲為 2019 年 7 月 2 日
# Date in test.yaml:
date: 07/02/2019
# Code to deserialize document to object:
var myObject= new DeserializerBuilder()
.WithNamingConvention(CamelCaseNamingConvention.Instance)
.Build()
.Deserialize<MyObject>(File.ReadAllText(args[0]));
有什么方法可以指定在使用 DeserializerBuilder 時應轉換日期時應使用 whc locate 嗎?
結果我需要包含對WithTypeConverter()
的調用並明確指定日期格式。
此代碼有效:
new DeserializerBuilder()
.WithNamingConvention(CamelCaseNamingConvention.Instance)
.WithTypeConverter(new DateTimeConverter(
provider: CultureInfo.CurrentCulture,
formats: new[] { "dd/MM/yyyy", "dd/MM/yyyy hh:mm" })
)
.Build()
.Deserialize<MyObject>(File.ReadAllText(args[0]));
我懷疑您需要明確地將文化設置為線程:
CultureInfo newCulture = CultureInfo.CreateSpecificCulture("en-GB");
Thread.CurrentThread.CurrentUICulture = newCulture;
// Maybe this one below isn't necessary...
Thread.CurrentThread.CurrentCulture = newCulture;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.