如何更新对象值(基于 curr-previous 模式)?

发布于 2024-09-28 06:03:49 字数 555 浏览 0 评论 0原文

假设您有一个具有以下简化结构的 CSV 文件,

 LINE1:  ID,Description,Value
 LINE2:  1,Product1,2
 LINE3:  ,,3
 LINE4:  ,,4
 LINE5:  2,Product2,2
 LINE6:  ,,3
 LINE7:  ,,5 

我正在使用 FileHelpers 来读取 CSV,并连接了一个接口,该接口允许我在读取当前行后访问该行。请参阅此SO问题了解更多背景信息。

问题是,使用这种方法我需要编写更多的 if 语句来检查所有需要复制的字段。 (我目前至少有 6 个具有相同“空白”格式的 csv 文件,所有文件都有 20 多个需要复制的字段 ~ 120 个 if 语句。urggh)

现在这不是一个微型文件优化练习,因为会有更多文件具有这种“不完整”格式。

如何以优雅的方式更新以前的记录,这样我就不必为每个字段编写 if 条件和声明?

Assume you have a CSV file with the following simplified structure

 LINE1:  ID,Description,Value
 LINE2:  1,Product1,2
 LINE3:  ,,3
 LINE4:  ,,4
 LINE5:  2,Product2,2
 LINE6:  ,,3
 LINE7:  ,,5 

I am using FileHelpers to read the CSV and have hooked up one the interfaces that allows me me to access the current line, after it has been read. Refer to this SO question for more background.

The issues is that using that approach I will need to write many more if statements to check all the fields that need to be copied. (I have at least, 6 csv files at the moment with the same 'blank' format all having more than 20 fields that need to be copied ~ 120 if statements. urggh)

Now this is not a micro optimisation exercise since there will be more files that will have this 'incomplete' format.

How can I update the previous record in an elegant way such that I wont have to write if conditions and declarations for each field?

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

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

发布评论

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

评论(1

-残月青衣踏尘吟 2024-10-05 06:03:49

目前的解决方案是使用名为 CopyMe 的自定义属性来注释必填字段,然后使用反射进行复制。

[DelimitedRecord(",") ]
[IgnoreFirst(1)] 
public class Product
{

    [CopyMe()] public int ID { get; set; }
    [CopyMe()] public string Description { get; set; }

    [FieldConverter(ConverterKind.Decimal)]
    public decimal Val{ get; set; }
}

AfterRead 方法看起来像这样......

public void AfterRead(AfterReadEventArgs<RawVolsDataPoints> e)
{
    var record = (Product)e.Record;

    if (PreviousRecord == null)
    {
        PreviousRecord = new Product();
         PreviousRecord = record;
    }

    if (String.IsNullOrEmpty(record.ID)) // null value indicates a new row
    {
        var ttype = typeof(Product);
        var fields = ttype.GetFields();

        var fieldsToCopy = fields.Where(field =>
            field.GetCustomAttributes(typeof(CopyMeAttribute), true).Any());

        foreach (var item in fieldsToCopy)
        {
            var prevvalue = item.GetValue(PreviousRecord);
            item.SetValue(record, prevvalue);
         }
    }
}

The current solution is to annotate the required fields using a custom attribute called CopyMe and then use reflection to copy.

[DelimitedRecord(",") ]
[IgnoreFirst(1)] 
public class Product
{

    [CopyMe()] public int ID { get; set; }
    [CopyMe()] public string Description { get; set; }

    [FieldConverter(ConverterKind.Decimal)]
    public decimal Val{ get; set; }
}

with the AfterRead method looking like so...

public void AfterRead(AfterReadEventArgs<RawVolsDataPoints> e)
{
    var record = (Product)e.Record;

    if (PreviousRecord == null)
    {
        PreviousRecord = new Product();
         PreviousRecord = record;
    }

    if (String.IsNullOrEmpty(record.ID)) // null value indicates a new row
    {
        var ttype = typeof(Product);
        var fields = ttype.GetFields();

        var fieldsToCopy = fields.Where(field =>
            field.GetCustomAttributes(typeof(CopyMeAttribute), true).Any());

        foreach (var item in fieldsToCopy)
        {
            var prevvalue = item.GetValue(PreviousRecord);
            item.SetValue(record, prevvalue);
         }
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文