我遇到了一個奇怪的問題,即 CsvHelper 不會更改使用類路徑來保存資料的屬性,例如set => Channel.Snippet.Title或set => Channel.Id.
using (StreamReader fileStream = new StreamReader(openFileDialog.FileName))
using (CsvReader reader = new CsvReader(fileStream, new CsvConfiguration(CultureInfo.InvariantCulture)
{
DetectColumnCountChanges = false,
HeaderValidated = (args) => // This is set because the CSV is only partial data
{
Console.WriteLine(args.ToString());
},
MissingFieldFound = null // This is set because the CSV is only partial data
}))
{
foreach (FullChannel record in reader.GetRecords<FullChannel>()) // tried adding .ToList()
{
Settings.Default.Channels.Add(record); // record.Id and record.Title are both null
Console.WriteLine($"Id: {record.Id} Title: {record.Title}"); // Logs: Id: Title:
record.Id = "foo"; // This works fine
record.Title = "bar"; // This works fine too
Console.WriteLine($"Id: {record.Id} Title: {record.Title}"); // Logs: Id: foo Title: bar
}
}
這總是反序列化正確的數量。我試圖創建一個新的TestChannel、干凈的和重建的。private string title如果沒有類路徑,例如使用in Title(注釋掉的代碼),CsvHelper 似乎能夠保存。
using Google.Apis.YouTube.v3.Data;
public class FullChannel
{
private string title;
public FullChannel()
{
Channel.Snippet = new ChannelSnippet();
}
// Functionality
public DateTime LastUpdated { get; set; }
public bool AllNotifications { get; set; }
// Channel
/// <summary>
/// Warning: Nothing in this property gets saved
/// </summary>
[XmlIgnore]
//public Channel Channel { get; set; } = new Channel(); // this is the original code
public TestChannel Channel { get; set; } = new TestChannel(); // this doesn't work for Id or Title
// For csv
[Name("Channel Id")]
public string Id { get => Channel.Id; set => Channel.Id = value; }
[Name("Channel Url")]
[XmlIgnore] // Generated value only
public string Url { get => $"https://www.youtube.com/channel/{Channel.Id}"; set => _ = value; }
[Name("Channel Title")]
public string Title {
//get => title;
//set => title = value;
get => Channel.Snippet.Title;
set => Channel.Snippet.Title = value;
}
}
public class TestChannel
{
public ChannelSnippet Snippet { get; set; }
public string Id { get; set; }
}
uj5u.com熱心網友回復:
我有一種感覺,CsvHelper 重構類的方式無法識別類路徑。我唯一可以建議的是直接保存到Channel.Snippet.Titleand Channel.Id。
void Main()
{
using (var fileStream = new StringReader("Channel Id,Channel Url,Channel Title\n1,this.com,MyTitle"))
using (CsvReader reader = new CsvReader(fileStream, new CsvConfiguration(CultureInfo.InvariantCulture)
{
DetectColumnCountChanges = false,
HeaderValidated = (args) => // This is set because the CSV is only partial data
{
Console.WriteLine(args.ToString());
},
MissingFieldFound = null // This is set because the CSV is only partial data
}))
{
reader.Context.RegisterClassMap<FullChannelMap>();
foreach (FullChannel record in reader.GetRecords<FullChannel>()) // tried adding .ToList()
{
Console.WriteLine($"Id: {record.Id} Title: {record.Title}"); // Logs: Id: Title:
record.Id = "foo"; // This works fine
record.Title = "bar"; // This works fine too
Console.WriteLine($"Id: {record.Id} Title: {record.Title}"); // Logs: Id: foo Title: bar
}
}
}
public class FullChannelMap : ClassMap<FullChannel>
{
public FullChannelMap()
{
Map(x => x.Channel.Id).Name("Channel Id");
Map(x => x.Channel.Snippet.Title).Name("Channel Title");
Map(x => x.Url).Name("Channel Url");
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/475896.html
上一篇:在本地而不是在服務器上保存檔案
