如何更新对象值(基于 curr-previous 模式)?
假设您有一个具有以下简化结构的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
目前的解决方案是使用名为
CopyMe
的自定义属性来注释必填字段,然后使用反射进行复制。AfterRead 方法看起来像这样......
The current solution is to annotate the required fields using a custom attribute called
CopyMe
and then use reflection to copy.with the AfterRead method looking like so...