csvhelper不设置具有类路径的属性
我有一个奇怪的问题,即CSVHelper不会更改使用ClassPath来保存数据的属性,例如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
,清洁和重建。如果没有类Path,EG使用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; }
}
I am having a strange issue where CsvHelper does not change properties that use classpaths to save data eg set => Channel.Snippet.Title
or 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(quot;Id: {record.Id} Title: {record.Title}"); // Logs: Id: Title:
record.Id = "foo"; // This works fine
record.Title = "bar"; // This works fine too
Console.WriteLine(quot;Id: {record.Id} Title: {record.Title}"); // Logs: Id: foo Title: bar
}
}
This always deserializes the correct amount. I've tried to create a new TestChannel
, clean and rebuild. CsvHelper seems to be able to save if there is no classpath eg using private string title
in Title
(The commented out code).
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 => quot;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; }
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我觉得CSVHelper重构班级没有认识到课堂路径。我唯一可以建议的是直接保存到
channel.snippet.title
和channel.id
。I have a feeling that the way CsvHelper reconstitutes the class is not recognizing the classpaths. The only thing I can suggest is saving directly to
Channel.Snippet.Title
andChannel.Id
.