目前,我正试图让我的CSV类从我的另一个类中为List<>属性写出列标题/值。由于某些原因,自动映射的功能无法识别该属性,因此它永远不会像我希望的那样写入我的CSV文件的末尾。对怎么做有什么想法吗?必须手动写到CSV吗?请让我知道。谢谢!
编辑:由于某些原因,我的List<>也返回为null,所以它不会通过自动映射编写。
public class TemplateViewModelMap<TViewModel> : ClassMap<TViewModel> where TViewModel : class
{
public TemplateViewModelMap()
{
AutoMap(CultureInfo.InvariantCulture);
// Use Reflection to check property for complex object type to remove/ignore from ClassMap.
PropertyInfo[] properties = typeof(TViewModel).GetProperties();
foreach (PropertyInfo property in properties)
{
string fieldPropertyName = "Fields";
if (property.Name.Equals(fieldPropertyName) == true)
{
MemberReferenceMap item = ReferenceMaps.Find(property);
ReferenceMaps.Add(item);
}
}
}发布于 2022-03-10 17:14:58
您是否试图映射一个具有字段而不是属性的类?在这种情况下,您可以将配置设置为MemberTypes.Fields。
void Main()
{
var fooList = new List<Foo>()
{
new Foo { Id = 1, Name = "first" },
new Foo { Id = 2, Name = "second" }
};
var config = new CsvConfiguration(CultureInfo.InvariantCulture)
{
MemberTypes = CsvHelper.Configuration.MemberTypes.Fields
};
using (var csv = new CsvWriter(Console.Out, config))
{
csv.WriteRecords(fooList);
}
}
public class Foo
{
public int Id;
public string Name;
}https://stackoverflow.com/questions/71427192
复制相似问题