2015-01-09 66 views
1

我有一个简单的列表包装类disappearring:列表<string>项目在C#

class ExtensionList 
{ 
    public List<string> Extensions; 

    public ExtensionList() 
    { 
     Extensions = new List<string>(); 
    } 

    public void push(params string[] ext) 
    { 
     // Add the * extension if "ALL__" is pushed. 
     if (ext.Contains("ALL__")) 
     { 
      Extensions = new List<string>(); 
      Extensions.Add("*"); 
      return; 
     } 

     // Don't add duplicate if pushed extension is already in list. 
     if (Extensions.Intersect(ext).Any()) 
      return; 

     Extensions.AddRange(ext); 
    } 
} 

和一个循环,实例化“录制的,其中的每一个具有上述列表中的对象之一。

public bool readAppConfig(string appConfigPath) 
    { 
     Recs = new Dictionary<string, List<Rec>>() 
     { 
      {"Headers", new List<Rec>()}, 
      {"Ignores", new List<Rec>()}, 
      {"Exceptions", new List<Rec>()} 
     }; 

     foreach (string sectionName in Recs.Keys) 
     { 
      var recSection = ConfigurationManager.GetSection(sectionName) as NameValueCollection; 
      foreach (string key in recSection.AllKeys) 
      { 
       Rec r = new Rec(); 
       r.strMatch = key; 
       r.ExtList.push(recSection[key].Split(';', ' ')); 
       Recs[sectionName].Add(r); 
      } 
     } 

     return true; 
    } 

问题很简单,而且很奇怪(至少对我来说)。 当调用r.ExtList.push(...)时,我可以使用visual studio执行push()函数,并看到正确的字符串被添加到ExtList的'Extensions'中(通过将它悬停在它上面)。然后,当我继续用visual studio逐步完成代码时,push()函数将返回到foreach循环。从那时起,这个Rec的ExtList不再有任何元素。只要push()返回,它们都消失了。

以下是Rec结构的完整性。

struct Rec 
{ 
    public string strMatch; 
    ExtensionList extList; 
    public ExtensionList ExtList { 
     get { return extList??new ExtensionList();} 
     set { extList = value; } 
    } 
}; 
+2

{返回extList ??新ExtensionList();}不分配给私人领域 – Zee 2015-01-09 18:38:42

回答

2

ExtList属性的属性返回值,如果为null,则返回新列表。然后调用该对象的getter并改变该列表。该列表在任何时候都没有设置为extList属性。虚空属性获得者,也不是初始化Rec的代码,永远不会设置该字段。令人惊讶的是,这实际上与你有一个可变结构的事实没有任何关系,即可变结构仍然是邪恶的,你应该避免它们像瘟疫一样。您的struct应该是不可变的,或者是class

0

我看不到ExtensionsList类的理由。如所介绍的,逻辑可以直接实施到Rec

public struct Rec 
{ 
    public Rec(string match) : this() 
    { 
     Match=match; 
     Extensions=new List<string>(); 
    } 
    public string Match { get; private set; } 
    public List<string> Extensions { get; private set; } 
    public void Push(params string[] ext) 
    { 
     if(ext.Contains("ALL__")) 
     { 
      Extensions=new List<string>() { "*" }; 
      return; 
     } 

     if(Extensions.Intersect(ext).Any()) 
     { 
      return; 
     } 

     Extensions.AddRange(ext); 
    } 
} 

类似的用法:

public bool readAppConfig(string appConfigPath) 
    { 
     Recs=new Dictionary<string, List<Rec>>() 
     { 
      {"Headers", new List<Rec>()}, 
      {"Ignores", new List<Rec>()}, 
      {"Exceptions", new List<Rec>()} 
     }; 

     foreach(string sectionName in Recs.Keys) 
     { 
      var recSection=ConfigurationManager.GetSection(sectionName) as NameValueCollection; 
      foreach(string key in recSection.AllKeys) 
      { 
       Rec r=new Rec(key); 
       r.Push(recSection[key].Split(';', ' ')); 
       Recs[sectionName].Add(r); 
      } 
     } 

     return true; 
    }