CSV Helper AutoMap 列表对象?

发布于 2025-01-13 12:54:50 字数 937 浏览 2 评论 0原文

我目前正在尝试让我的 CSV Helper AutoMap 类写出 List<> 的列标题/值。我的其他班级的财产。由于某种原因,自动映射功能无法识别此属性,因此它永远不会像我希望的那样写入 CSV 文件的末尾。关于如何做到这一点有什么想法吗?是否必须手动写入CSV?请告诉我。谢谢!

编辑:我的列表>>由于某种原因,这里也返回为 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);
            }
        }
}

I am currently trying to get my CSV Helper AutoMap class to write out column headers/values for a List<> property from my other Class. For some reason, the functionality for auto mapping doesn't recognize this property, so it never gets written to the end of my CSV file like I want it to. Any thoughts on how to do this? Does it have to be written to CSV manually? Please let me know. Thanks!

EDIT: My List<> also comes back as null for some reason here so it doesn't get written via auto mapping.

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

苍风燃霜 2025-01-20 12:54:50

您是否正在尝试映射一个具有字段但没有属性的类?在这种情况下,您可以将配置设置为 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;
}

Are you trying to map a class that has fields and not properties? In that case you can set the configuration to 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;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文