C#:列表中填充了 class 的相同实例,而不是新实例

C#: List is filled with the same instance of a class instead of new ones

我正在尝试从内存流中读取文件并将其内容组织到实例列表中。文件中的某些行具有相应的类型类,文件中可以出现多个相同类型的行。

我当前的代码如下所示:

       public void initializeStructure( System.IO.MemoryStream RawFile )
       { 
            int positioninFile = 0;
            while (!reachedEndOfFile)
            {
                string recordType = GetRecordType(RawFile, positioninFile);
                int ListPosition = MessageStructure.Count;

                switch (recordType)
                {
                    case "01":
                        Class_01 _01 = new Class_01(Params);
                        MessageStructure.Add(_01);
                        break;
                    case "02":
                        Class_02 _02 = new Class_02(Params);
                        MessageStructure.Add(_02);
                        break;
                    case "04":
                        Class_04 _04 = new Class_04(Params);
                        MessageStructure.Add(_04);
                        break;
                    default:
                        reachedEndOfFile = true;
                        break;
                }

                string row = GetRowFromMemoryStream(RawFile, 572, positioninFile);
                MessageStructure[ListPosition].WriteRule(row);

                ListPosition++;
                positioninFile += 572;
            }
       }

消息结构定义:

public List<A_Record> MessageStructure = new List<A_Record>();

这些 类 外观示例:

    public class Class_01 : A_Record
    {
        public Class_01( Dictionary<string, A_AbstractClass> Params ) : base(Params)
        {
            RecordType = "01";
            RecordTitle = "Name of record";
        }

    }

A_Record摘要

    public abstract class A_Record
    {
       public virtual string RecordType { get; set; }
       public virtual string RecordTitle { get; set; }

        public int RecordLength { get; set; }

        public void WriteRule( string Vektis_Rule )
        {
            int writePosition = 0;

            foreach ( KeyValuePair<string, A_Element> Element in Elementen )
            {
                string ElementValue = Vektis_Rule.Substring( writePosition, Element.Value.Length );
                Element.Value.Write( ElementValue );

                writePosition += Element.Value.Length;
            }
        }

        public A_Record(Dictionary<string, A_Gegevenselement> Volgnummers)
        {
            Elementen = Volgnummers;
            RecordLength = 0;

            foreach (KeyValuePair<string, A_Gegevenselement> Element in Elementen)
            {
                var currentElement = Elementen[Element.Key];
                currentElement.StartingPosition = RecordLength;
                RecordLength += currentElement.Length;
            }
        }

    }

所以基本上,对于出现的每个“04”行,我想向列表中添加一个新的 Class_04 实例,然后将该行的内容写入该实例。

遗憾的是,这并没有像我希望的那样工作。如果我将 Class_04 的多个实例添加到列表中,它们都具有最后添加的 Class_04 行的值。这可能意味着列表中的所有 Class_04 条目都是同一个实例,我不断地覆盖它而不是添加一个新实例。但是我不明白这是怎么回事,看起来我每次都在 While 循环中创建一个新实例。

有人可以帮助我吗?就像你知道的那样,直到最近,我以前只使用 PHP、Python 和 Javascript。所以我可能只是在这里度过了一个 d'oh 时刻并且遗漏了一些非常明显的东西。

提前感谢您的时间和精力!

ListPosition,您在循环开始时将其设置为计数,因此您的增量被覆盖或多余。 没有 MessageStructure 的代码,看不出来,但看起来不对。

我会完全摆脱 ListPosition,并使用 [MessageStructure.Count] - 1 或更好的 MessageStructure.Last() 而不是索引。

我怀疑文件末尾也会出错,因为切换后的代码将执行并且应该包含在 if (reachedEndOfFile)

我无法从这里的代码中看出,但我怀疑您没有为每个记录对象的参数实例化一个新的 Dictionary。在 A_Record class 中,你有这一行: Elementen = Volgnummers; 这存储了对你传入的字典的引用。如果你将同一个字典实例与另一个 A_Record例如,它们将具有相同的内容。