CsvHelper没有设置具有类路径的属性



我有一个奇怪的问题,CsvHelper不改变使用类路径来保存数据的属性,例如set => Channel.Snippet.Titleset => 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,清洁和重建。CsvHelper似乎能够保存,如果没有类路径,例如在Title中使用private string title(注释掉的代码)。

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; }
}

我有一种感觉,CsvHelper重构类的方式不能识别类路径。我唯一能建议的是直接保存到Channel.Snippet.TitleChannel.Id

void Main()
{
using (var fileStream = new StringReader("Channel Id,Channel Url,Channel Titlen1,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");
}
}

相关内容

  • 没有找到相关文章

最新更新