我有一个简单的List包装类:
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);
}
}
和一个实例化'Rec'的循环,每个循环都有一个上面的列表对象。
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; }
}
};
答案 0 :(得分:2)
ExtList
属性的getter返回值,或者如果它为null,则返回一个新列表。然后,您调用该对象的getter并改变该列表。在任何时候都没有将该列表设置为extList
属性。更糟糕的是属性获取器,以及初始化Rec
的代码,无论如何设置该字段。
令人惊讶的是,这实际上并没有与你有一个可变结构这一事实有关,即可变结构仍然是邪恶的,你应该像瘟疫一样避免它们。您的struct
应该是不可变的,或class
。
答案 1 :(得分: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;
}