0

这个问题是这个问题的一个衍生问题(可以将此方法重构为使用 Lamba 吗?)。在对该问题的评论中提出了这一点:

重构需要在不同的地方进行。糟糕的不是你的函数,而是CLMExplorerHistory.

内部数据结构不需要以 1:1 的方式表示 CSV 文件的结构。只需确保您可以以兼容的方式进行读写。

我认为您正在尝试直接使用 csv 数据结构,而不是查看可能的重复区域,这些区域表明实现类类型的明显位置以允许代码重用。您能否为您的 CSV 文件发布一段示例数据以表明您的起点?


在这些评论的后端,这里是示例 CSV 数据的片段:

Date,Status,Weekly Bible Reading,Song1 #,Song1 Title,Song1 Scripture,Song2 #,Song2 Title,Song2 Scripture,Song3 #,Song3 Title,Song3 Scripture,Meeting?,CO Visit,Cancel Reason,# Classes,Chairman,PrayerOpen,PrayerClose,TreasuresTalk,TreasuresTalk_Theme,TreasuresDigging,BibleReading_A,BibleReading_Study_A,BibleReading_B,BibleReading_Study_B,BibleReading_C,BibleReading_Study_C,BibleReading_Source,Apply1_Description,Apply2_Description,Apply3_Description,Apply4_Description,Apply1_A,Apply1_Asst_A,Apply1_Study_A,Apply2_A,Apply2_Asst_A,Apply2_Study_A,Apply3_A,Apply3_Asst_A,Apply3_Study_A,Apply4_A,Apply4_Asst_A,Apply4_Study_A,Apply1_B,Apply1_Asst_B,Apply1_Study_B,Apply2_B,Apply2_Asst_B,Apply2_Study_B,Apply3_B,Apply3_Asst_B,Apply3_Study_B,Apply4_B,Apply4_Asst_B,Apply4_Study_B,Apply1_C,Apply1_Asst_C,Apply1_Study_C,Apply2_C,Apply2_Asst_C,Apply2_Study_C,Apply3_C,Apply3_Asst_C,Apply3_Study_C,Apply4_C,Apply4_Asst_C,Apply4_Study_C,LivingPart1,LivingPart1_Theme,LivingPart1_Length,LivingPart2,LivingPart2_Theme,LivingPart2_Length,CBS,CBS_Source,CBS_Read,Audience B,Audience C,AuxCounselor B,AuxCounselor C
01/03/2021,Registrato e completo,NUMERI 7-8,4,“Geova è il mio Pastore”,Salmo 23,54,“Questa è la via”,"Isaia 30:20, 21",127,Che tipo di persona sono?,2 Pietro 3:11,Y,N,,1,Italo De Gaeta,xxx,xxx,xxx,“L’accampamento d’Israele: lezioni utili”,xxx,xxx,5,,,,,Nu 7:1-17,Commemorazione,Visita ulteriore,Visita ulteriore,Visita ulteriore,xxx,xxx,11,xxx,xxx,6,xxx,xxx,12,xxx,xxx,17,,,,,,,,,,,,,,,,,,,,,,,,,xxx,Risultati raggiunti dall’organizzazione,(5 min),xxx,Bisogni locali,(10 min),xxx,"rr cap. 5 parr. 17-22, riquadro 5A",xxx,,,,

我使用CsvReader该类将其读入CLMExplorerHistory对象。该类定义如下:

public class CLMExplorerHistory
{
    [Format("dd/MM/yyyy")]
    [Name("Date")]
    public DateTime Date { get; set; }

    [BooleanFalseValues(new string[] { "N", "n" })]
    [BooleanTrueValues(new string[] { "Y", "y" })]
    [Name("Meeting?")]
    public bool Meeting { get; set; }

    [Name("# Classes")]
    public int Classes { get; set; }

    [Name("Chairman")]
    public string Chairman { get; set; }

    [Name("AuxCounselor B")]
    public string AuxCounsellor1 { get; set; }

    [Name("AuxCounselor C")]
    public string AuxCounsellor2 { get; set; }

    [Name("PrayerOpen")]
    public string PrayerOpen { get; set; }

    [Name("PrayerClose")]
    public string PrayerClose { get; set; }

    [Name("CBS")]
    public string CBSConductor { get; set; }

    [Name("CBS_Read")]
    public string CBSReader { get; set; }

    [Name("TreasuresTalk")]
    public string TreasuresTalkName { get; set; }

    [Name("TreasuresTalk_Theme")]
    public string TreasuresTalkTheme { get; set; }

    [Name("TreasuresDigging")]
    public string SpiritualGemsName { get; set; }

    [Name("LivingPart1")]
    public string LivingPart1Name { get; set; }

    [Name("LivingPart1_Theme")]
    public string LivingPart1Theme { get; set; }

    [Name("LivingPart2")]
    public string LivingPart2Name { get; set; }

    [Name("LivingPart2_Theme")]
    public string LivingPart2Theme { get; set; }

    [Name("BibleReading_A")]
    public string BibleReadingClass1Name { get; set; }

    [Name("BibleReading_B")]
    public string BibleReadingClass2Name { get; set; }

    [Name("BibleReading_C")]
    public string BibleReadingClass3Name { get; set; }

    [Name("BibleReading_Study_A")]
    public string BibleReadingStudy { get; set; }

    [Name("Apply1_Description")]
    public string StudentItem1Description { get; set; }

    [Name("Apply2_Description")]
    public string StudentItem2Description { get; set; }

    [Name("Apply3_Description")]
    public string StudentItem3Description { get; set; }

    [Name("Apply4_Description")]
    public string StudentItem4Description { get; set; }

    [Name("Apply1_A")]
    public string StudentItem1Class1StudentName { get; set; }

    [Name("Apply1_B")]
    public string StudentItem1Class2StudentName { get; set; }

    [Name("Apply1_C")]
    public string StudentItem1Class3StudentName { get; set; }

    [Name("Apply1_Asst_A")]
    public string StudentItem1Class1AssistantName { get; set; }

    [Name("Apply1_Asst_B")]
    public string StudentItem1Class2AssistantName { get; set; }

    [Name("Apply1_Asst_C")]
    public string StudentItem1Class3AssistantName { get; set; }

    [Name("Apply2_A")]
    public string StudentItem2Class1StudentName { get; set; }

    [Name("Apply2_B")]
    public string StudentItem2Class2StudentName { get; set; }

    [Name("Apply2_C")]
    public string StudentItem2Class3StudentName { get; set; }

    [Name("Apply2_Asst_A")]
    public string StudentItem2Class1AssistantName { get; set; }

    [Name("Apply2_Asst_B")]
    public string StudentItem2Class2AssistantName { get; set; }

    [Name("Apply2_Asst_C")]
    public string StudentItem2Class3AssistantName { get; set; }

    [Name("Apply3_A")]
    public string StudentItem3Class1StudentName { get; set; }

    [Name("Apply3_B")]
    public string StudentItem3Class2StudentName { get; set; }

    [Name("Apply3_C")]
    public string StudentItem3Class3StudentName { get; set; }

    [Name("Apply3_Asst_A")]
    public string StudentItem3Class1AssistantName { get; set; }

    [Name("Apply3_Asst_B")]
    public string StudentItem3Class2AssistantName { get; set; }

    [Name("Apply3_Asst_C")]
    public string StudentItem3Class3AssistantName { get; set; }

    [Name("Apply4_A")]
    public string StudentItem4Class1StudentName { get; set; }

    [Name("Apply4_B")]
    public string StudentItem4Class2StudentName { get; set; }

    [Name("Apply4_C")]
    public string StudentItem4Class3StudentName { get; set; }

    [Name("Apply4_Asst_A")]
    public string StudentItem4Class1AssistantName { get; set; }

    [Name("Apply4_Asst_B")]
    public string StudentItem4Class2AssistantName { get; set; }

    [Name("Apply4_Asst_C")]
    public string StudentItem4Class3AssistantName { get; set; }

    [Name("Apply1_Study_A")]
    public string StudentItem1Study { get; set; }

    [Name("Apply2_Study_A")]
    public string StudentItem2Study { get; set; }

    [Name("Apply3_Study_A")]
    public string StudentItem3Study { get; set; }

    [Name("Apply4_Study_A")]
    public string StudentItem4Study { get; set; }
}

我像这样读入 CSV 文件:

using (var reader = new StreamReader(_calendarDBPath))
using (var csv = new CsvReader(reader, CultureInfo.InvariantCulture))
{
    var records = csv.GetRecords<CLMExplorerHistory>();

    foreach (var record in records)
    {
        ...

目前,CSV 记录已读入此公共属性列表。我想知道它是否可以直接将字段读入更复杂的对象?

具体来说,该# Classes字段的值为 1、2 或 3。对于这些类中的每一个(不是 C# 类 - 想想school),都有类似的数据。

每个班级有 4 个项目,每个班级的描述/研究都是相同的。所以这些 CSV 字段很常见:

[Name("Apply1_Description")]
[Name("Apply2_Description")]
[Name("Apply3_Description")]
[Name("Apply4_Description")]

[Name("BibleReading_Study_A")]
[Name("Apply1_Study_A")]
[Name("Apply2_Study_A")]
[Name("Apply3_Study_A")]
[Name("Apply4_Study_A")]

但其余的是特定于 3 个班级(学校):

  • 这些字段属于第 1 类:
[Name("BibleReading_A")]
[Name("Apply1_A")]
[Name("Apply1_Asst_A")]
[Name("Apply2_A")]
[Name("Apply2_Asst_A")]
[Name("Apply3_A")]
[Name("Apply3_Asst_A")]
[Name("Apply4_A")]
[Name("Apply4_Asst_A")]
  • 这些字段属于第 2 类:
[Name("BibleReading_B")]
[Name("Apply1_B")]
[Name("Apply1_Asst_B")]
[Name("Apply2_B")]
[Name("Apply2_Asst_B")]
[Name("Apply3_B")]
[Name("Apply3_Asst_B")]
[Name("Apply4_B")]
[Name("Apply4_Asst_B")]
  • 这些字段属于第 3 类:
[Name("BibleReading_C")]
[Name("Apply1_C")]
[Name("Apply1_Asst_C")]
[Name("Apply2_C")]
[Name("Apply2_Asst_C")]
[Name("Apply3_C")]
[Name("Apply3_Asst_C")]
[Name("Apply4_C")]
[Name("Apply4_Asst_C")]

这 3 个类可以由 3 个“StudentClass”对象表示Lists。就像是

StudentClasses
    NumClasses
    Item1Desc
    Item2Desc
    Item3Desc
    Item4Desc
    BibleReadingStudy
    Item1Study
    Item2Study
    Item3Study
    Item4Study
    List<StudentClass>
           BibleReading
           Item1Student
           Item1Assistant
           Item2Student
           Item2Assistant
           Item3Student
           Item3Assistant
           Item4Student
           Item4Assistant

以上是由以下内容构建的:

StudentClasses
    NumClasses:        # Classes
    Item1Desc:         Apply1_Description
    Item2Desc:         Apply2_Description
    Item3Desc:         Apply3_Description
    Item4Desc:         Apply4_Description
    BibleReadingStudy: BibleReading_Study_A
    Item1Study:        Apply1_Study_A
    Item2Study:        Apply2_Study_A
    Item3Study:        Apply3_Study_A
    Item4Study:        Apply4_Study_A
    List<StudentClass>
           BibleReading    BibleReading_A|B|C
           Item1Student    Apply1_A|B|C
           Item1Assistant  Apply1_Asst_A|B|C
           Item2Student    Apply2_A|B|C
           Item2Assistant  Apply2_Asst_A|B|C
           Item3Student    Apply3_A|B|C
           Item3Assistant  Apply3_Asst_A|B|C
           Item4Student    Apply4_A|B|C
           Item5Assistant  Apply4_Asst_A|B|C

现在您知道了字段与建议对象之间的映射。显然,我可以自己创建该 C# 类,并且可以手动将字段转换为这样的结构。但是CvsReader有没有像这样直接读入的能力呢?而不是一大堆属性?

4

2 回答 2

2

但是CvsReader有没有像这样直接读入类的能力呢?而不是一大堆属性?

我会说“是的”,虽然我不能 100% 确定你最终希望创造什么,但你可以对这些属性所做的事情变得可爱。我举一个简单的例子。

假设我们有一个 CSV,其中列出了商店和需要从每个商店购买的 3 种产品

Shop,Product1,P1Price,Product2,P2Price,Product3,P3Price
Walmart,Eggs,1,Milk,2,Bread,3
BestBuy,PC,4,iPad,5,iPhone,6

你可以让你的班级持有道具:

class ShopLine
{
  string Shop {get;set;}
  string Product1 {get;set;}
  string Product2 {get;set;}
  string Product3 {get;set;}
  int P1Price {get;set;}
  int P2Price {get;set;}
  int P3Price {get;set;}
}

但是没有人拿枪指着你说数据存储必须是这样的......你可以:

class Product
{
  string Name {get;set;}
  int Price {get;set;}
}

class ShopLineTrick
{
  string Shop {get;set;}
  string Product1 {get => products[0].Name; set => products[0].Name= value;}
  string Product2 {get => products[1].Name; set => products[1].Name= value;}
  string Product3 {get => products[2].Name; set => products[2].Name= value;}
  int P1Price {get => products[0].Price; set => products[0].Price = value;}
  int P2Price {get => products[1].Price; set => products[1].Price = value;}
  int P3Price {get => products[2].Price; set => products[2].Price = value;}

  List<Product> products;

  ShopLineTrick(){
     //precreate 3 products
     products = Enumerable.Range(1,3).Select(x => new Product()).ToList();
  }
}

在操作结束时,您有一些 ShopLineTrick 集合,每个集合都包含一些构建的 Product 层次结构;产品是“来自其他地方的可重用类”,所以这个操作就像一个解析和映射。以前用于接收文件中数据的 POCO 已成为一种能够从其他地方构建较小的、可重用类的层次结构的设备。例如,您可以使用 LINQ 查询以获取它们

shopLines.SelectMany(sl => sl.Products). // a straight list of every product

我已经使用替代示例进行了解释,因为我还不太了解您希望使用您拥有的数据实现什么:我正在描述替代数据存储,以便您可以将其应用于进一步实现您的目标。例如,对于最近的一个项目,我必须在某个时间点存储属性的每个版本,然后让我无法控制的进程更改对象的某些属性,然后检查发生了什么变化。这在循环中很容易做到,10 个属性中的每一个都使用一个对象[10] 进行存储。在某个时间点,我会克隆数组,让进程更改内容,然后在当前查找与克隆副本不同的数组条目。使用数组支持属性通过循环数组使检查(甚至重置/接受更改)变得微不足道


唯一需要注意的是 CsvH 的某些读取方式会重用您传递的对象,因此如果您正在执行异步读取,请注意它可以读取文件的所有行并且您最终会仅包含最后一个中的数据(您可以提取产品列表,并在每次循环通过时更新它以防止覆盖)

于 2022-01-13T10:00:04.470 回答
0

该答案基于已接受答案中的原则。我觉得我会添加它作为我特定场景的扩展解释。它可能会帮助其他人。


步骤1

我设计了一组类来代表一个会议周:

public class MSAHistoryItemTalk
{
    public string Name { get; set; }
    public string Theme { get; set; }
    public string Method { get; set; }
}
public class MSAHistoryItemStudent
{
    public string Name { get; set; }
    public string Assistant { get; set; }
    public string Type { get; set; }
    public string Study { get; set; }
}

public class MSAHistoryWeek
{
    public DateTime Week { get; set; }
    public bool Meeting { get; set; }
    public int NumClasses { get; set; }
    public string Host { get; set; }
    public string Cohost { get; set; }
    public string Chairman { get; set; }
    public string AuxCounsellor1 { get; set; }
    public string AuxCounsellor2 { get; set; }
    public string PrayerOpen { get; set; }
    public string PrayerClose { get; set; }
    public string CBSConductor { get; set; }
    public string CBSReader { get; set; }
    public List<MSAHistoryItemTalk> TalkItems { get; set; }
    public List<string> Teaching { get; set; }
    public List<string> StudentItemStudyNumbers { get; set; }
    public List<string> StudentItemDescriptions { get; set; }
    public int NumStudentItems { get; set; }
    public List<MSAHistoryItemStudent>[] StudentItems { get; set; }
    public MSAHistoryWeek()
    {
        TalkItems = Enumerable.Range(1, 6).Select(x => new MSAHistoryItemTalk()).ToList();
        Teaching = Enumerable.Range(1, 3).Select(x => string.Empty).ToList();
        StudentItemStudyNumbers = Enumerable.Range(1, 5).Select(x => string.Empty).ToList();
        StudentItemDescriptions = Enumerable.Range(1, 5).Select(x => string.Empty).ToList();
        StudentItems = new List<MSAHistoryItemStudent>[]
        {
            Enumerable.Range(1, 5).Select(x => new MSAHistoryItemStudent()).ToList(),
            Enumerable.Range(1, 5).Select(x => new MSAHistoryItemStudent()).ToList(),
            Enumerable.Range(1, 5).Select(x => new MSAHistoryItemStudent()).ToList(),
        };
    }

    public override string ToString()
    {
        return Week.ToShortDateString();
    }
}

第2步

我将对象的一个​​实例添加MSAHistoryWeek到使用的类的修改版本中CsvReader

public class CLMExplorerHistory
{
    [Format("dd/MM/yyyy")]
    [Name("Date")]
    public DateTime Date { get => HistoryWeek.Week; set => HistoryWeek.Week = value; }

    [BooleanFalseValues(new string[] { "N", "n" })]
    [BooleanTrueValues(new string[] { "Y", "y" })]
    [Name("Meeting?")]
    public bool Meeting { get => HistoryWeek.Meeting; set => HistoryWeek.Meeting = value; }

    [Name("# Classes")]
    public int Classes { get => HistoryWeek.NumClasses; set => HistoryWeek.NumClasses = value; }

    [Name("Chairman")]
    public string Chairman { get => HistoryWeek.Chairman; set => HistoryWeek.Chairman = value; }

    [Name("AuxCounselor B")]
    public string AuxCounsellor1 { get => HistoryWeek.AuxCounsellor1; set => HistoryWeek.AuxCounsellor1 = value; }

    [Name("AuxCounselor C")]
    public string AuxCounsellor2 { get => HistoryWeek.AuxCounsellor2; set => HistoryWeek.AuxCounsellor2 = value; }

    [Name("PrayerOpen")]
    public string PrayerOpen { get => HistoryWeek.PrayerOpen; set => HistoryWeek.PrayerOpen = value; }

    [Name("PrayerClose")]
    public string PrayerClose { get => HistoryWeek.PrayerClose; set => HistoryWeek.PrayerClose = value; }

    [Name("CBS")]
    public string CBSConductor { get => HistoryWeek.CBSConductor; set => HistoryWeek.CBSConductor = value; }

    [Name("CBS_Read")]
    public string CBSReader { get => HistoryWeek.CBSReader; set => HistoryWeek.CBSReader = value; }

    [Name("TreasuresTalk")]
    public string TreasuresTalkName { get => HistoryWeek.TalkItems[0].Name; set => HistoryWeek.TalkItems[0].Name = value; }

    [Name("TreasuresTalk_Theme")]
    public string TreasuresTalkTheme { get => HistoryWeek.TalkItems[0].Theme; set => HistoryWeek.TalkItems[0].Theme = value; }

    [Name("TreasuresDigging")]
    public string SpiritualGemsName { get => HistoryWeek.TalkItems[1].Name; set => HistoryWeek.TalkItems[1].Name = value; }

    [Name("LivingPart1")]
    public string LivingPart1Name { get => HistoryWeek.TalkItems[3].Name; set => HistoryWeek.TalkItems[3].Name = value; }

    [Name("LivingPart1_Theme")]
    public string LivingPart1Theme { get => HistoryWeek.TalkItems[3].Theme; set => HistoryWeek.TalkItems[3].Theme = value; }

    [Name("LivingPart2")]
    public string LivingPart2Name { get => HistoryWeek.TalkItems[4].Name; set => HistoryWeek.TalkItems[4].Name = value; }

    [Name("LivingPart2_Theme")]
    public string LivingPart2Theme { get => HistoryWeek.TalkItems[4].Theme; set => HistoryWeek.TalkItems[4].Theme = value; }

    [Name("BibleReading_A")]
    public string BibleReadingClass1Name { get => HistoryWeek.StudentItems[0][0].Name; set => HistoryWeek.StudentItems[0][0].Name = value; }

    [Name("BibleReading_B")]
    public string BibleReadingClass2Name { get => HistoryWeek.StudentItems[1][0].Name; set => HistoryWeek.StudentItems[1][0].Name = value; }

    [Name("BibleReading_C")]
    public string BibleReadingClass3Name { get => HistoryWeek.StudentItems[2][0].Name; set => HistoryWeek.StudentItems[2][0].Name = value; }

    [Name("BibleReading_Study_A")]
    public string BibleReadingStudy { get => HistoryWeek.StudentItemStudyNumbers[0]; set => HistoryWeek.StudentItemStudyNumbers[0] = value; }

    [Name("Apply1_Description")]
    public string StudentItem1Description { get => HistoryWeek.StudentItemDescriptions[1]; set => HistoryWeek.StudentItemDescriptions[1] = value; }

    [Name("Apply2_Description")]
    public string StudentItem2Description { get => HistoryWeek.StudentItemDescriptions[2]; set => HistoryWeek.StudentItemDescriptions[2] = value; }

    [Name("Apply3_Description")]
    public string StudentItem3Description { get => HistoryWeek.StudentItemDescriptions[3]; set => HistoryWeek.StudentItemDescriptions[3] = value; }

    [Name("Apply4_Description")]
    public string StudentItem4Description { get => HistoryWeek.StudentItemDescriptions[4]; set => HistoryWeek.StudentItemDescriptions[4] = value; }

    [Name("Apply1_A")]
    public string StudentItem1Class1StudentName { get => HistoryWeek.StudentItems[0][1].Name; set => HistoryWeek.StudentItems[0][1].Name = value; }

    [Name("Apply1_B")]
    public string StudentItem1Class2StudentName { get => HistoryWeek.StudentItems[1][1].Name; set => HistoryWeek.StudentItems[1][1].Name = value; }

    [Name("Apply1_C")]
    public string StudentItem1Class3StudentName { get => HistoryWeek.StudentItems[2][1].Name; set => HistoryWeek.StudentItems[2][1].Name = value; }

    [Name("Apply1_Asst_A")]
    public string StudentItem1Class1AssistantName { get => HistoryWeek.StudentItems[0][1].Assistant; set => HistoryWeek.StudentItems[0][1].Assistant = value; }

    [Name("Apply1_Asst_B")]
    public string StudentItem1Class2AssistantName { get => HistoryWeek.StudentItems[1][1].Assistant; set => HistoryWeek.StudentItems[1][1].Assistant = value; }

    [Name("Apply1_Asst_C")]
    public string StudentItem1Class3AssistantName { get => HistoryWeek.StudentItems[2][1].Assistant; set => HistoryWeek.StudentItems[2][1].Assistant = value; }

    [Name("Apply2_A")]

    public string StudentItem2Class1StudentName { get => HistoryWeek.StudentItems[0][2].Name; set => HistoryWeek.StudentItems[0][2].Name = value; }

    [Name("Apply2_B")]
    public string StudentItem2Class2StudentName { get => HistoryWeek.StudentItems[1][2].Name; set => HistoryWeek.StudentItems[1][2].Name = value; }

    [Name("Apply2_C")]
    public string StudentItem2Class3StudentName { get => HistoryWeek.StudentItems[2][2].Name; set => HistoryWeek.StudentItems[2][2].Name = value; }

    [Name("Apply2_Asst_A")]
    public string StudentItem2Class1AssistantName { get => HistoryWeek.StudentItems[0][2].Assistant; set => HistoryWeek.StudentItems[0][2].Assistant = value; }

    [Name("Apply2_Asst_B")]
    public string StudentItem2Class2AssistantName { get => HistoryWeek.StudentItems[1][2].Assistant; set => HistoryWeek.StudentItems[1][2].Assistant = value; }

    [Name("Apply2_Asst_C")]
    public string StudentItem2Class3AssistantName { get => HistoryWeek.StudentItems[2][2].Assistant; set => HistoryWeek.StudentItems[2][2].Assistant = value; }

    [Name("Apply3_A")]
    public string StudentItem3Class1StudentName { get => HistoryWeek.StudentItems[0][3].Name; set => HistoryWeek.StudentItems[0][3].Name = value; }

    [Name("Apply3_B")]
    public string StudentItem3Class2StudentName { get => HistoryWeek.StudentItems[1][3].Name; set => HistoryWeek.StudentItems[1][3].Name = value; }

    [Name("Apply3_C")]
    public string StudentItem3Class3StudentName { get => HistoryWeek.StudentItems[2][3].Name; set => HistoryWeek.StudentItems[2][3].Name = value; }

    [Name("Apply3_Asst_A")]
    public string StudentItem3Class1AssistantName { get => HistoryWeek.StudentItems[0][3].Assistant; set => HistoryWeek.StudentItems[0][3].Assistant = value; }

    [Name("Apply3_Asst_B")]
    public string StudentItem3Class2AssistantName { get => HistoryWeek.StudentItems[1][3].Assistant; set => HistoryWeek.StudentItems[1][3].Assistant = value; }

    [Name("Apply3_Asst_C")]
    public string StudentItem3Class3AssistantName { get => HistoryWeek.StudentItems[2][3].Assistant; set => HistoryWeek.StudentItems[2][3].Assistant = value; }

    [Name("Apply4_A")]
    public string StudentItem4Class1StudentName { get => HistoryWeek.StudentItems[0][4].Name; set => HistoryWeek.StudentItems[0][4].Name = value; }

    [Name("Apply4_B")]
    public string StudentItem4Class2StudentName { get => HistoryWeek.StudentItems[1][4].Name; set => HistoryWeek.StudentItems[1][4].Name = value; }

    [Name("Apply4_C")]
    public string StudentItem4Class3StudentName { get => HistoryWeek.StudentItems[2][4].Name; set => HistoryWeek.StudentItems[2][4].Name = value; }

    [Name("Apply4_Asst_A")]
    public string StudentItem4Class1AssistantName { get => HistoryWeek.StudentItems[0][4].Assistant; set => HistoryWeek.StudentItems[0][4].Assistant = value; }

    [Name("Apply4_Asst_B")]
    public string StudentItem4Class2AssistantName { get => HistoryWeek.StudentItems[1][4].Assistant; set => HistoryWeek.StudentItems[1][4].Assistant = value; }

    [Name("Apply4_Asst_C")]
    public string StudentItem4Class3AssistantName { get => HistoryWeek.StudentItems[2][4].Assistant; set => HistoryWeek.StudentItems[2][4].Assistant = value; }

    [Name("Apply1_Study_A")]
    public string StudentItem1Study { get => HistoryWeek.StudentItemStudyNumbers[1]; set => HistoryWeek.StudentItemStudyNumbers[1] = value; }

    [Name("Apply2_Study_A")]
    public string StudentItem2Study { get => HistoryWeek.StudentItemStudyNumbers[2]; set => HistoryWeek.StudentItemStudyNumbers[2] = value; }

    [Name("Apply3_Study_A")]
    public string StudentItem3Study { get => HistoryWeek.StudentItemStudyNumbers[3]; set => HistoryWeek.StudentItemStudyNumbers[3] = value; }

    [Name("Apply4_Study_A")]
    public string StudentItem4Study { get => HistoryWeek.StudentItemStudyNumbers[4]; set => HistoryWeek.StudentItemStudyNumbers[4] = value; }

    [Ignore]
    public MSAHistoryWeek HistoryWeek;

    public CLMExplorerHistory()
    {
        HistoryWeek = new MSAHistoryWeek();
    }
}

如您所见,它遵循公认答案中的原则,将公共 CSV 属性映射到增强MSAHistoryWeek对象。


结果

这种重构使处理数据变得更加容易(即写入 XML)。

于 2022-01-13T20:48:15.597 回答