如何在 C# 中创建具有不同输入数量的 Class 列表

How to create a Class List with different numbers of inputs in C#

我正在处理我的第一个真正的 c# 项目,我在基于 Class 创建列表的方式上遇到了问题,我不知道如何解决。

我正在尝试编写一些代码,它采用多层结构的输入文件 (txt/csv),将其放入我的程序中,然后将结构写入新的 txt/csv 文件.

当层数相同时,效果很好。但是,当结构的层数不同时,就会出现问题,我会得到一个“System.IndexOutOfRangeException”。 我的问题是:我能否使我的列表所基于的 Class 成为动态的(我不知道这是否是技术术语),以便它适用于不同数量的输入?将构造添加到程序时和将其写入新文件时?

我的代码是:

class Program
{
    static void Main(string[] args)
    {
        // Filepath for the input and output file
        string filePathIn_constructions = @"C:\Library\Constructions.txt";
        string filePathOut = @"C:\Library\EPlus_Inputfile.txt";

        // Creating a list of constructions based on the class. The list is made from the file "filePathIn_constructions"
        List<Construction> allConstructions = new List<Construction>();
        List<string> lines_constructions = File.ReadAllLines(filePathIn_constructions).ToList(); // add it to a list

        // Adding all the data from the fil to the variable "allConstructions"
        foreach (var line in lines_constructions)
        {
            string[] entries = line.Split(',');

            Construction newConstruction = new Construction();
            newConstruction.EIndex = entries[0];
            newConstruction.Name = entries[1];
            newConstruction.Layer1 = entries[2];
            newConstruction.Layer2 = entries[3];
            newConstruction.Layer3 = entries[4];
            newConstruction.Layer4 = entries[5];
            newConstruction.Layer5 = entries[6];
            
            allConstructions.Add(newConstruction); // Add it to our list of constructions
        }

        List<string> output = new List<string>();

        foreach (var x in allConstructions) // Printing the new 
        {
            output.Add($"{x.EIndex}, {x.Name}, {x.Layer1}, {x.Layer2}, {x.Layer3}, {x.Layer4}, {x.Layer5}");
            
        }
        File.WriteAllLines(txtFilePathOut, output);
    }
}

我的 Class 建筑是

public class Construction
{
    public string EIndex { get; set; }
    public string Name { get; set; }
    public string Layer1 { get; set; }
    public string Layer2 { get; set; }
    public string Layer3 { get; set; }
    public string Layer4 { get; set; }
    public string Layer5 { get; set; }      
}

input/output 文件的示例可以是

Construction,ConcreteWall,Concrete;
Construction,Brickwall1,Birck,Isulation,Brick;
Construction,Brickwall2,Birck,AirGap,Isulation,Brick;
Construction,Wood/Concrete Wall,Wood,Isulation,Concrete,Gypson;
Construction,Wood Wall,Wood,AirGap,Gypson,Isulaiton,Gypson;

希望有人能帮忙。谢谢

编辑:我必须能够单独地超出构造名称,因为我正在使用它来进行一些排序。

这是因为您正试图访问不存在的数组单元格 (documentation)

在您的 input/output 文件中,您的行具有 3 到 7 个值,并且您正在使用这些值构建一个数组 entries。这意味着您将拥有包含 3 到 7 个单元格的数组

问题是,在创建这些数组后,您尝试访问每个数组 单元格 0、1、2... 直到第 7 个,即使对于只有 3 个单元格的数组!

您可以通过简单的方式解决此问题,即添加列以在每行上使用相同数量的分隔符(您将行的分隔符定义为带有 line.Split(',') 的列)。这样,您将创建的每个数组将始终具有 7 个单元格,即使其中的值为 null

public class Construction
{
    public string EIndex { get; set; }
    public string Name { get; set; }
    public List<string> Layers { get; set; } = new List<string>();     
}

        foreach (var line in lines_constructions)
        {
            string[] entries = line.Split(',');

            Construction newConstruction = new Construction();
            newConstruction.EIndex = entries[0];
            newConstruction.Name = entries[1];
            for (int i=2; i < entries.Length; i++) {
               newConstruction.Layers.Add(entries[i]); 
            }
            
            allConstructions.Add(newConstruction);
        }

        foreach(var x in allConstuctions) {
           File.AppendAllText(output, $"{x.EIndex}, {x.Name}, {string.Join(", ", x.Layers)}");
        }