c# - 创建新元素将删除 C# 列表中的第一个元素

标签 c# list

我创建了一个类 -WhiteList - 包含从 xml 文件读取的数据。我想列出这些元素,但在将第一个元素添加到列表后,我为数据创建了一个新元素,它删除列表中第一个元素的内容。 IE。似乎 new (elements) 重新创建了对列表中元素的引用。

代码片段:

namespace WhiteList
{
   public class WhiteListElement
   {
      private const byte EQL = 0;  
      private const byte CTN = 1;  
      private const byte COMMON_NAME = 0;  
      private const byte ORG = 1;  
      private const byte ORG_UNIT = 2;  
      private const byte LOC = 3;  
      private const byte STATE = 4; 
      private const byte COUNTRY = 5;   

      private static string[,] Subject;  
      private static string[,] Issuer;   

      private static string MinTlsLevel; 
      private static string Customer;


      public WhiteListElement()
      {
         Subject = new string[6, 2];
         Issuer = new string[6, 2];
         Customer = "";
         MinTlsLevel = "";
      }

      //---- set/get functions ---- example
      public string GetCommonName(bool SubjectVal, bool Name)
      {  
         if (true == SubjectVal) { if (true == Name) return Subject[COMMON_NAME, 0]; else return Subject[COMMON_NAME, 1]; }
         else { if (true == Name) return Issuer[COMMON_NAME, 0]; else return Issuer[COMMON_NAME, 1]; }
      }
      public void SetCommonName(bool SubjectVal, bool Name, string NewValue)
      {  
         if (true == SubjectVal) { if (true == Name) Subject[COMMON_NAME, 0] = NewValue; else Subject[COMMON_NAME, 1] = NewValue; }
         else { if (true == Name) Issuer[COMMON_NAME, 0] = NewValue; else Issuer[COMMON_NAME, 1] = NewValue; }
      }

   }
   class Program
   {
      public static void CreateWhiteList()
      {
         try
         {
            using (XmlReader reader = XmlReader.Create("WhiteList.xml"))
            {
               while (reader.Read())
               {
                  if (reader.NodeType == XmlNodeType.Element)
                  {
                     if (reader.Name == "Kunder")
                     {
                        while (reader.Read())
                        {
                           if (reader.NodeType == XmlNodeType.Element)
                           {
                              XElement el = (XElement)XNode.ReadFrom(reader);
                              if (el != null)
                              {
                                 WhiteListElement elem = new WhiteListElement();

                                 var noderef = el.FirstNode;
                                 elem.SetCustomer(el.Name.ToString());
                                 while (noderef.NextNode != null)
                                 {
                                    noderef = noderef.NextNode;
                                    string nodestring = noderef.ToString();
                                    if (nodestring[0] == '<')
                                    {
                                       int startindx, stopindx;
                                       string tag = nodestring.Substring(3);
                                       string data;
                                       tag = tag.Substring(0, tag.IndexOf('_'));
                                       startindx = nodestring.IndexOf('\"') + 1;
                                       stopindx = (nodestring.Substring(startindx)).IndexOf('\"');
                                       data = nodestring.Substring(startindx, stopindx);
                                       switch (tag)
                                       {
                                          case "CERTLVL": elem.SetCertLvl(data); break;     
                                          case "CN": if (nodestring[1] == 'S') elem.SetCommonName(true, true, data); if (nodestring[1] == 'I') elem.SetCommonName(false, true, data); break;
                                          case "OU": if (nodestring[1] == 'S') elem.SetOrgUnit(true, true, data); if (nodestring[1] == 'I') elem.SetOrgUnit(false, true, data); break;
                                          case "O": if (nodestring[1] == 'S') elem.SetOrg(true, true, data); if (nodestring[1] == 'I') elem.SetOrg(false, true, data); break;
                                          case "L": if (nodestring[1] == 'S') elem.SetLocation(true, true, data); if (nodestring[1] == 'I') elem.SetLocation(false, true, data); break;
                                          case "S": if (nodestring[1] == 'S') elem.SetState(true, true, data); if (nodestring[1] == 'I') elem.SetState(false, true, data); break;
                                          case "C": if (nodestring[1] == 'S') elem.SetCountry(true, true, data); if (nodestring[1] == 'I') elem.SetCountry(false, true, data); break;
                                       }
                                    }

                                 }


                                 CustomerList.Add(elem);
                              }
                           }
                        }
                     }
                  }
               }
            }
         }
         catch (Exception ex) { Console.WriteLine(ex.Message); throw (ex); }
      }

      public static List<WhiteListElement>CustomerList = null;

      static void Main(string[] args)
      {
         CustomerList = new List<WhiteListElement>();
         CreateWhiteList();
      }
   }
}

------------ 代码片段结束。

问题发生在第一个元素被放入列表 (CustomerList.Add(elem)) 并返回到行“WhiteListElement elem = new WhiteListElement();”之后。这将删除 CustomerList[0] 中的元素,之后将数据放入 elem 时,它会插入到 elem 和 CustomerList[0] 中,最终在列表中有两个相同的元素。

我什至尝试在添加 elem 之后放置一个 elem = null 来尝试删除引用,但这没有用

我做错了什么?

/卡斯滕

最佳答案

  private static string[,] Subject;  
  private static string[,] Issuer;   

  private static string MinTlsLevel; 
  private static string Customer;

你的问题来自这里,静态成员通过一个类的所有实例共享,因此当你编辑第二个时,你删除第一个,你的类应该是这样的

public class WhiteListElement
   {
      private const byte EQL = 0;  
      private const byte CTN = 1;  
      private const byte COMMON_NAME = 0;  
      private const byte ORG = 1;  
      private const byte ORG_UNIT = 2;  
      private const byte LOC = 3;  
      private const byte STATE = 4; 
      private const byte COUNTRY = 5;   

      private string[,] Subject;  
      private string[,] Issuer;   

      private string MinTlsLevel; 
      private string Customer;


      public WhiteListElement()
      {
         Subject = new string[6, 2];
         Issuer = new string[6, 2];
         Customer = "";
         MinTlsLevel = "";
      }

      //---- set/get functions ---- example
      public string GetCommonName(bool SubjectVal, bool Name)
      {  
         if (true == SubjectVal) { if (true == Name) return Subject[COMMON_NAME, 0]; else return Subject[COMMON_NAME, 1]; }
         else { if (true == Name) return Issuer[COMMON_NAME, 0]; else return Issuer[COMMON_NAME, 1]; }
      }
      public void SetCommonName(bool SubjectVal, bool Name, string NewValue)
      {  
         if (true == SubjectVal) { if (true == Name) Subject[COMMON_NAME, 0] = NewValue; else Subject[COMMON_NAME, 1] = NewValue; }
         else { if (true == Name) Issuer[COMMON_NAME, 0] = NewValue; else Issuer[COMMON_NAME, 1] = NewValue; }
      }

   }

编辑:您可以在引用资料中阅读更多关于静态的内容:https://msdn.microsoft.com/fr-fr/library/98f28cdx.aspx

尤其是:

While an instance of a class contains a separate copy of all instance fields of the class, there is only one copy of each static field.

It is not possible to use this to reference static methods or property accessors.

关于c# - 创建新元素将删除 C# 列表中的第一个元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36081424/

相关文章:

perl - 在 Perl 中实现一个节点列表

C# DateTime 未正确格式化为字符串

c# - 使用 DataTable 填充 C# TreeView

c# - 在 Windows 窗体上绘制单个像素

python - 与此等效的 MySql Update 是什么?

list - FTP LIST命令返回的数据格式?

c# - xml 错误 : Object reference not set to an instance of an object after SelectSingleNode

c# - PowerBI AADSTS90002 : Tenant authorize not found

python - 在 Python 中初始化对象列表

python - 在另一个列表python中搜索列表项的最佳方法?